Comment 4 for bug 617591

Revision history for this message
Gennady Proskurin (gpr) wrote :

We use safeInc/Dec for different purposes:
1. reference counting in smart pointers
2. BufferedSocket - counting for "sockets" variable
3. Client.h/struct Counts - for just counters which incremented/decremented from different threads

My patch is for (1) only. It uses atomic counter which was designed exactly for this purpose - reference counting in smart pointers (it has necessary memory barriers in case it reaches zero).

For (2) - in current code I think we also can use the same atomic_count, since we do some action when "sockets" variable reaches zero. But if sometimes we will use it in form like "if (sockets==1) do_something()", it will not be safe.

For (3) it is not obviously immediately, if we can replace it with atomic counter, since memory barriers may be necessary.

As you can see, we use safeInc/Dec for different purposes, so it may require different "optimized" implementations. For (1), it is very straightforward, for other cases it is not.