}
DWORD
-pthread::cancelable_wait (HANDLE object, DWORD timeout, const bool do_cancel,
- const bool do_sig_wait)
+cancelable_wait (HANDLE object, DWORD timeout, const bool do_cancel,
+ const enum cw_sig_wait sig_wait)
{
DWORD res;
DWORD num = 0;
HANDLE wait_objects[3];
- pthread_t thread = self ();
+ pthread_t thread = pthread::self ();
/* Do not change the wait order.
The object must have higher priority than the cancel event,
if both objects are signaled. */
wait_objects[num++] = object;
DWORD cancel_n;
- if (!is_good_object (&thread) ||
+ if (!pthread::is_good_object (&thread) ||
thread->cancelstate == PTHREAD_CANCEL_DISABLE)
cancel_n = (DWORD) -1;
else
}
DWORD sig_n;
- if (!do_sig_wait || &_my_tls != _main_tls)
+ if (sig_wait == cw_sig_nosig || &_my_tls != _main_tls)
sig_n = (DWORD) -1;
else
{
wait_objects[sig_n] = signal_arrived;
}
- res = WaitForMultipleObjects (num, wait_objects, FALSE, timeout);
- if (res == sig_n - WAIT_OBJECT_0)
- res = WAIT_SIGNALED;
- else if (res == cancel_n - WAIT_OBJECT_0)
+ while (1)
{
- if (do_cancel)
- pthread::static_cancel_self ();
- res = WAIT_CANCELED;
+ res = WaitForMultipleObjects (num, wait_objects, FALSE, timeout);
+ res -= WAIT_OBJECT_0;
+ if (res == cancel_n)
+ {
+ if (do_cancel)
+ pthread::static_cancel_self ();
+ res = WAIT_CANCELED;
+ }
+ else if (res != sig_n)
+ /* all set */;
+ else if (sig_wait == cw_sig_eintr)
+ res = WAIT_SIGNALED;
+ else
+ {
+ _my_tls.call_signal_handler ();
+ continue;
+ }
+ break;
}
return res;
}
++mutex->condwaits;
mutex->unlock ();
- rv = pthread::cancelable_wait (sem_wait, dwMilliseconds, false, true);
+ rv = cancelable_wait (sem_wait, dwMilliseconds, false, cw_sig_eintr);
mtx_out.lock ();
waitlength -= tv.tv_sec * 1000 + tv.tv_usec / 1000;
if (waitlength < 0)
waitlength = 0;
- switch (pthread::cancelable_wait (win32_obj_id, waitlength, true, true))
+ switch (cancelable_wait (win32_obj_id, waitlength, true, cw_sig_eintr))
{
case WAIT_OBJECT_0:
currentvalue--;
int
semaphore::_wait ()
{
- switch (pthread::cancelable_wait (win32_obj_id, INFINITE, true, true))
+ switch (cancelable_wait (win32_obj_id, INFINITE, true, cw_sig_eintr))
{
case WAIT_OBJECT_0:
currentvalue--;
(*thread)->attr.joinable = PTHREAD_CREATE_DETACHED;
(*thread)->mutex.unlock ();
- bool loop = false;
- do
- switch (cancelable_wait ((*thread)->win32_obj_id, INFINITE, false, true))
- {
- case WAIT_OBJECT_0:
- if (return_val)
- *return_val = (*thread)->return_ptr;
- delete (*thread);
- break;
- case WAIT_SIGNALED:
- _my_tls.call_signal_handler ();
- loop = true;
- break;
- case WAIT_CANCELED:
- // set joined thread back to joinable since we got canceled
- (*thread)->joiner = NULL;
- (*thread)->attr.joinable = PTHREAD_CREATE_JOINABLE;
- joiner->cancel_self ();
- // never reached
- break;
- default:
- // should never happen
- return EINVAL;
- }
- while (loop);
+ switch (cancelable_wait ((*thread)->win32_obj_id, INFINITE, false, cw_sig_resume))
+ {
+ case WAIT_OBJECT_0:
+ if (return_val)
+ *return_val = (*thread)->return_ptr;
+ delete (*thread);
+ break;
+ case WAIT_CANCELED:
+ // set joined thread back to joinable since we got canceled
+ (*thread)->joiner = NULL;
+ (*thread)->attr.joinable = PTHREAD_CREATE_JOINABLE;
+ joiner->cancel_self ();
+ // never reached
+ break;
+ default:
+ // should never happen
+ return EINVAL;
+ }
}
return 0;
#include <security.h>
#include <errno.h>
+enum cw_sig_wait
+{
+ cw_sig_nosig,
+ cw_sig_eintr,
+ cw_sig_resume
+};
+
extern "C"
{
void SetResourceLock (int, int, const char *) __attribute__ ((regparm (3)));
__attribute__ ((regparm (3)));
}
+DWORD cancelable_wait (HANDLE, DWORD, const bool = true, const enum cw_sig_wait = cw_sig_nosig)
+ __attribute__ ((regparm (3)));
+
class fast_mutex
{
public:
void lock ()
{
- if (InterlockedIncrement ((long *)&lock_counter) != 1)
- WaitForSingleObject (win32_obj_id, INFINITE);
+ if (InterlockedIncrement ((long *) &lock_counter) != 1)
+ cancelable_wait (win32_obj_id, INFINITE, false, cw_sig_resume);
}
void unlock ()
{
- if (InterlockedDecrement ((long *)&lock_counter))
+ if (InterlockedDecrement ((long *) &lock_counter))
::ReleaseSemaphore (win32_obj_id, 1, NULL);
}
virtual void testcancel ();
static void static_cancel_self ();
- static DWORD cancelable_wait (HANDLE object, DWORD timeout, const bool do_cancel = true, const bool do_sig_wait = false);
-
virtual int setcancelstate (int state, int *oldstate);
virtual int setcanceltype (int type, int *oldtype);