summaryrefslogtreecommitdiff
path: root/stats/shared_item_pool.h
blob: 89dc9b350b7a4102c62420ec8b12134f62ed9979 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* SPDX-License-Identifier: Apache-2.0 */
/*
 * Copyright (C) 2014-2018 Intel Corporation
 *
 * This implementation is highly derived from ChromeOS:
 *   platform2/camera/hal/intel/ipu3/common/SharedItemPool.h
 */

#ifndef SHARED_ITEM_POOL_H
#define SHARED_ITEM_POOL_H

#include <memory>
#include <mutex>
#include <pthread.h>
#include <vector>

/**
 * \class SharedItemPool
 *
 * Pool of ref counted items. This class creates a pool of items and manages
 * the acquisition of them. When all references to this item have disappeared
 * The item is returned to the pool.
 *
 * This class is thread safe, i.e. it can be called from  multiple threads.
 * When the element is recycled to the pool it can be reset via a client
 * provided method.
 *
 */

namespace libcamera {

template<class ItemType>
class SharedItemPool
{
public:
	SharedItemPool(const char *name = "Unnamed");
	~SharedItemPool();

	/**
	 * Initializes the capacity of the pool. It allocates the objects.
	 * optionally it will take function to reset the item before recycling
	 * it to the pool.
	 * This method is thread safe.
	 *
	 * \param capacity[IN]: Number of items the pool will hold
	 * \param resetter[IN]: Function to reset the item before recycling to the
	 *                      pool.
	 * \return -ENOSYS if trying to initialize twice
	 * \return 0 If everything went ok.
	 */
	int init(int32_t capacity, void (*resetter)(ItemType *) = nullptr);

	bool isFull();

	/**
	 * Free the resources of the pool
	 *
	 * \return 0 on success
	 */
	int deInit();

	/**
	 * Acquire an item from the pool.
	 * This method is thread safe. Access to the internal acquire/release
	 * methods are protected.
	 * BUT the thread-safety for the utilization of the item after it has been
	 * acquired is the user's responsibility.
	 * Be careful not to provide the same item to multiple threads that write
	 * into it.
	 *
	 * \param item[OUT] shared pointer to an item.
	 * \return 0 on success
	 */
	int acquireItem(std::shared_ptr<ItemType> &item);

	/**
	 * Returns the number of currently available items
	 * It there would be issues acquiring the lock the method returns 0
	 * available items.
	 *
	 * \return item count
	 */
	size_t availableItems();

private:
	int _releaseItem(ItemType *item);

	class ItemDeleter
	{
	public:
		ItemDeleter(SharedItemPool *pool)
			: mPool(pool) {}
		void operator()(ItemType *item) const
		{
			mPool->_releaseItem(item);
		}

	private:
		SharedItemPool *mPool;
	};

	std::vector<ItemType *> available_; /* SharedItemPool doesn't have ownership */
	ItemType *allocated_;
	size_t capacity_;
	ItemDeleter deleter_;
	std::mutex mutex_; /* protects available_, allocated_, capacity_ */
	const char *poolName_;
	void (*resetter_)(ItemType *);
};

} /* namespace libcamera */

#endif /* SHARED_ITEM_POOL_H */