2003-01-09 Thomas Pfaff <tpfaff@gmx.net>
+ * thread.h (WAIT_CANCELED): New define.
+ (pthread::cancelable_wait): New static method.
+ * thread.cc (pthread::cancelable_wait): Implement.
+ (semaphore::Wait): Wait on semaphore and thread cancellation.
+ (pthread::join): Wait on joined thread and thread cancellation.
+ (semaphore::wait): Add testcancel to check for thread
+ cancellation even if the semaphore is available.
+
+2003-01-09 Thomas Pfaff <tpfaff@gmx.net>
+
* include/pthread.h: Add define for errorchecking mutexes.
Change default mutex type.
* thread.cc (pthread_cond::TimedWait): Update mutex unlock
read ()
readv ()
select ()
-sem_wait ()
+*sem_wait ()
sigpause ()
sigsuspend ()
sigtimedwait ()
}
+DWORD pthread::cancelable_wait (HANDLE object, DWORD timeout, const bool do_cancel)
+{
+ DWORD res;
+ HANDLE wait_objects[2];
+ pthread_t thread = self ();
+
+ if (!isGoodObject (&thread) || thread->cancelstate == PTHREAD_CANCEL_DISABLE)
+ return WaitForSingleObject (object, timeout);
+
+ // Do not change the wait order
+ // The object must have higher priority than the cancel event,
+ // because WaitForMultipleObjects will return the smallest index
+ // if both objects are signaled
+ wait_objects[0] = object;
+ wait_objects[1] = thread->cancel_event;
+
+ res = WaitForMultipleObjects (2, wait_objects, FALSE, timeout);
+ if (do_cancel && res == WAIT_CANCELED)
+ pthread::static_cancel_self ();
+ return res;
+}
+
int
pthread::setcancelstate (int state, int *oldstate)
{
void
semaphore::Wait ()
{
- WaitForSingleObject (win32_obj_id, INFINITE);
- currentvalue--;
+ switch (pthread::cancelable_wait (win32_obj_id, INFINITE))
+ {
+ case WAIT_OBJECT_0:
+ currentvalue--;
+ break;
+ default:
+ debug_printf ("cancelable_wait failed. %E");
+ return;
+ }
}
void
{
pthread_t joiner = self ();
- if (!isGoodObject (&joiner))
- return EINVAL;
+ joiner->testcancel ();
// Initialize return val with NULL
if (return_val)
*return_val = NULL;
- /* FIXME: wait on the thread cancellation event as well - we are a cancellation point*/
+ if (!isGoodObject (&joiner))
+ return EINVAL;
+
if (!isGoodObject (thread))
return ESRCH;
(*thread)->joiner = joiner;
(*thread)->attr.joinable = PTHREAD_CREATE_DETACHED;
(*thread)->mutex.UnLock ();
- WaitForSingleObject ((*thread)->win32_obj_id, INFINITE);
- if (return_val)
- *return_val = (*thread)->return_ptr;
- // cleanup
- delete (*thread);
- } /* End if */
- pthread_testcancel ();
+ switch (cancelable_wait ((*thread)->win32_obj_id, INFINITE, false))
+ {
+ 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;
}
int
semaphore::wait (sem_t *sem)
{
+ pthread_testcancel ();
+
if (!isGoodObject (sem))
{
set_errno (EINVAL);
static nativeMutex mutexInitializationLock;
};
+#define WAIT_CANCELED (WAIT_OBJECT_0 + 1)
+
class pthread:public verifyable_object
{
public:
virtual void testcancel ();
static void static_cancel_self ();
+ static DWORD cancelable_wait (HANDLE object, DWORD timeout, const bool do_cancel = true);
+
virtual int setcancelstate (int state, int *oldstate);
virtual int setcanceltype (int type, int *oldtype);