mirror of
https://github.com/zeromq/libzmq.git
synced 2025-10-27 11:06:52 +01:00
Problem: need atomic reference counting in several projects
Solution: as libzmq already provides this across all platforms, expose an atomic counter API. I've not wrapped atomic pointers, though someone who needs this may want to do so.
This commit is contained in:
@@ -22,6 +22,8 @@
|
||||
#include "clock.hpp"
|
||||
#include "err.hpp"
|
||||
#include "thread.hpp"
|
||||
#include "atomic_counter.hpp"
|
||||
#include "atomic_ptr.hpp"
|
||||
#include <assert.h>
|
||||
#include "../include/zmq_utils.h"
|
||||
|
||||
@@ -204,3 +206,50 @@ int zmq_curve_keypair (char *z85_public_key, char *z85_secret_key)
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Initialize a new atomic counter, which is set to zero
|
||||
|
||||
void *zmq_atomic_counter_new (void)
|
||||
{
|
||||
zmq::atomic_counter_t *counter = new zmq::atomic_counter_t;
|
||||
alloc_assert (counter);
|
||||
return counter;
|
||||
}
|
||||
|
||||
// Se the value of the atomic counter
|
||||
|
||||
void zmq_atomic_counter_set (void *counter_, int value_)
|
||||
{
|
||||
((zmq::atomic_counter_t *) counter_)->set (value_);
|
||||
}
|
||||
|
||||
// Increment the atomic counter, and return the old value
|
||||
|
||||
int zmq_atomic_counter_inc (void *counter_)
|
||||
{
|
||||
return ((zmq::atomic_counter_t *) counter_)->add (1);
|
||||
}
|
||||
|
||||
// Decrement the atomic counter and return true if still > zero
|
||||
|
||||
bool zmq_atomic_counter_dec (void *counter_)
|
||||
{
|
||||
return ((zmq::atomic_counter_t *) counter_)->sub (1);
|
||||
}
|
||||
|
||||
// Return actual value of atomic counter
|
||||
|
||||
int zmq_atomic_counter_value (void *counter_)
|
||||
{
|
||||
return ((zmq::atomic_counter_t *) counter_)->get ();
|
||||
}
|
||||
|
||||
// Destroy atomic counter, and set reference to NULL
|
||||
|
||||
void zmq_atomic_counter_destroy (void **counter_p_)
|
||||
{
|
||||
delete ((zmq::atomic_counter_t *) *counter_p_);
|
||||
*counter_p_ = NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user