1

When working with SBCL semaphores (sb-thread package) I can easily check the current semaphore count, as demonstrated by the snippet below:

CL-USER> (defvar *semaphore* (sb-thread:make-semaphore))
*SEMAPHORE*
CL-USER> (sb-thread::semaphore-count *semaphore*)
0
CL-USER> (sb-thread::signal-semaphore *semaphore*)
NIL
CL-USER> (sb-thread::semaphore-count *semaphore*)
1

I've been trying to do the same in Clozure CL (using the ccl package) but was unable to. Here's an example:

CL-USER> (defvar *semaphore* (ccl:make-semaphore))
*SEMAPHORE*
CL-USER> (ccl::semaphore-value *semaphore*)
#<A Foreign Pointer [gcable] #x7F6B240050B0>

When I inspect the Foreign Pointer, this is what I see:

#<MACPTR #x30200080B31D>
--------------------
@0=#<A Foreign Pointer [gcable] #x7F6B240050B0>

Underlying UVECTOR

I was unable to find any way to access the internal counter, nor to find any substantial hint to that effect. I've been thinking about perhaps using the foreign function interface and making use of the sem_getvalue system call, but do not yet know how to go about it. Do you have any suggestions or insights as to how to access this counter? Any help greatly appreciated.

Wojciech Gac
  • 1,538
  • 1
  • 16
  • 30
  • I guess the developers didn't bother implementing a function to check the count since it's usually not needed and the value you get is possibly outdated anyway. – jkiiski May 26 '16 at 07:59
  • What do you mean 'outdated', @jkiiski? Because of the way semaphores are used, the mechanism must certainly be able to determine if the counter is zero or not in order to know how to handle a `signal-semaphore`. – Wojciech Gac May 26 '16 at 08:04
  • 1
    If you get the current count in one thread, and another thread increments or decrements it right after, the count you got in the first thread will be wrong. – jkiiski May 26 '16 at 08:11
  • Ah, right. Sorry, haven't slept too much last night. – Wojciech Gac May 26 '16 at 08:18
  • @WojciechGac What platform are you on? It looks like CCL may use OS-native semaphores, which might change how and whether you'd be able to access the count. – Joshua Taylor May 26 '16 at 13:30
  • @JoshuaTaylor, I tested this on Kali GNU/Linux, which is a derivative of Debian. – Wojciech Gac May 26 '16 at 17:15
  • CCL doesn't seem to have a function for that. You might want to ask on the CCL mailing list, if somebody has implemented it or if the maintainers would be willing to provide it. – Rainer Joswig May 27 '16 at 17:26

0 Answers0