Skip to content

Commit

Permalink
Fix NEED_SEM code (WinCE)
Browse files Browse the repository at this point in the history
  • Loading branch information
rpj committed Apr 25, 2005
1 parent 150a7a6 commit c2c2749
Show file tree
Hide file tree
Showing 11 changed files with 1,808 additions and 1,864 deletions.
1,308 changes: 651 additions & 657 deletions implement.h

Large diffs are not rendered by default.

111 changes: 56 additions & 55 deletions private.c
Original file line number Diff line number Diff line change
@@ -1,55 +1,56 @@
/*
* private.c
*
* Description:
* This translation unit implements routines which are private to
* the implementation and may be used throughout it.
*
* --------------------------------------------------------------------------
*
* Pthreads-win32 - POSIX Threads Library for Win32
* Copyright(C) 1998 John E. Bossom
* Copyright(C) 1999,2005 Pthreads-win32 contributors
*
* Contact Email: [email protected]
*
* The current list of contributors is contained
* in the file CONTRIBUTORS included with the source
* code distribution. The list can also be seen at the
* following World Wide Web location:
* http://sources.redhat.com/pthreads-win32/contributors.html
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library in the file COPYING.LIB;
* if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/

#include "pthread.h"
#include "implement.h"

/* Must be first to define HAVE_INLINABLE_INTERLOCKED_CMPXCHG */
#include "ptw32_InterlockedCompareExchange.c"

#include "ptw32_is_attr.c"
#include "ptw32_processInitialize.c"
#include "ptw32_processTerminate.c"
#include "ptw32_threadStart.c"
#include "ptw32_threadDestroy.c"
#include "ptw32_tkAssocCreate.c"
#include "ptw32_tkAssocDestroy.c"
#include "ptw32_callUserDestroyRoutines.c"
#include "ptw32_semwait.c"
#include "ptw32_timespec.c"
#include "ptw32_throw.c"
#include "ptw32_getprocessors.c"
/*
* private.c
*
* Description:
* This translation unit implements routines which are private to
* the implementation and may be used throughout it.
*
* --------------------------------------------------------------------------
*
* Pthreads-win32 - POSIX Threads Library for Win32
* Copyright(C) 1998 John E. Bossom
* Copyright(C) 1999,2005 Pthreads-win32 contributors
*
* Contact Email: [email protected]
*
* The current list of contributors is contained
* in the file CONTRIBUTORS included with the source
* code distribution. The list can also be seen at the
* following World Wide Web location:
* http://sources.redhat.com/pthreads-win32/contributors.html
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library in the file COPYING.LIB;
* if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/

#include "pthread.h"
#include "implement.h"

/* Must be first to define HAVE_INLINABLE_INTERLOCKED_CMPXCHG */
#include "ptw32_InterlockedCompareExchange.c"

#include "ptw32_is_attr.c"
#include "ptw32_processInitialize.c"
#include "ptw32_processTerminate.c"
#include "ptw32_threadStart.c"
#include "ptw32_threadDestroy.c"
#include "ptw32_tkAssocCreate.c"
#include "ptw32_tkAssocDestroy.c"
#include "ptw32_callUserDestroyRoutines.c"
#include "ptw32_semwait.c"
#include "ptw32_timespec.c"
#include "ptw32_relmillisecs.c"
#include "ptw32_throw.c"
#include "ptw32_getprocessors.c"
255 changes: 118 additions & 137 deletions ptw32_semwait.c
Original file line number Diff line number Diff line change
@@ -1,137 +1,118 @@
/*
* ptw32_semwait.c
*
* Description:
* This translation unit implements mutual exclusion (mutex) primitives.
*
* --------------------------------------------------------------------------
*
* Pthreads-win32 - POSIX Threads Library for Win32
* Copyright(C) 1998 John E. Bossom
* Copyright(C) 1999,2005 Pthreads-win32 contributors
*
* Contact Email: [email protected]
*
* The current list of contributors is contained
* in the file CONTRIBUTORS included with the source
* code distribution. The list can also be seen at the
* following World Wide Web location:
* http://sources.redhat.com/pthreads-win32/contributors.html
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library in the file COPYING.LIB;
* if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/

#ifndef _UWIN
//# include <process.h>
#endif
#include "pthread.h"
#include "implement.h"


int
ptw32_semwait (sem_t * sem)
/*
* ------------------------------------------------------
* DESCRIPTION
* This function waits on a POSIX semaphore. If the
* semaphore value is greater than zero, it decreases
* its value by one. If the semaphore value is zero, then
* the calling thread (or process) is blocked until it can
* successfully decrease the value.
*
* Unlike sem_wait(), this routine is non-cancelable.
*
* RESULTS
* 0 successfully decreased semaphore,
* -1 failed, error in errno.
* ERRNO
* EINVAL 'sem' is not a valid semaphore,
* ENOSYS semaphores are not supported,
* EINTR the function was interrupted by a signal,
* EDEADLK a deadlock condition was detected.
*
* ------------------------------------------------------
*/
{
int result = 0;
DWORD status;
sem_t s = *sem;

if (s == NULL)
{
result = EINVAL;
}
else
{

#ifdef NEED_SEM

status = WaitForSingleObject (s->event, INFINITE);

#else /* NEED_SEM */

if ((result = pthread_mutex_lock (&s->lock)) == 0)
{
int v = --s->value;

(void) pthread_mutex_unlock (&s->lock);

if (v < 0)
{
/* Must wait */
status = WaitForSingleObject (s->sem, INFINITE);
}
else
{
return 0;
}
}
else
{
return result;
}

#endif

if (status == WAIT_OBJECT_0)
{

#ifdef NEED_SEM

ptw32_decrease_semaphore (sem);

#endif /* NEED_SEM */

return 0;
}
else
{
#ifndef NEED_SEM
(void) InterlockedIncrement((LPLONG) &s->value);
#endif
result = EINVAL;
}
}

if (result != 0)
{
errno = result;
return -1;
}

return 0;

} /* ptw32_semwait */
/*
* ptw32_semwait.c
*
* Description:
* This translation unit implements mutual exclusion (mutex) primitives.
*
* --------------------------------------------------------------------------
*
* Pthreads-win32 - POSIX Threads Library for Win32
* Copyright(C) 1998 John E. Bossom
* Copyright(C) 1999,2005 Pthreads-win32 contributors
*
* Contact Email: [email protected]
*
* The current list of contributors is contained
* in the file CONTRIBUTORS included with the source
* code distribution. The list can also be seen at the
* following World Wide Web location:
* http://sources.redhat.com/pthreads-win32/contributors.html
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library in the file COPYING.LIB;
* if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/

#ifndef _UWIN
//# include <process.h>
#endif
#include "pthread.h"
#include "implement.h"


int
ptw32_semwait (sem_t * sem)
/*
* ------------------------------------------------------
* DESCRIPTION
* This function waits on a POSIX semaphore. If the
* semaphore value is greater than zero, it decreases
* its value by one. If the semaphore value is zero, then
* the calling thread (or process) is blocked until it can
* successfully decrease the value.
*
* Unlike sem_wait(), this routine is non-cancelable.
*
* RESULTS
* 0 successfully decreased semaphore,
* -1 failed, error in errno.
* ERRNO
* EINVAL 'sem' is not a valid semaphore,
* ENOSYS semaphores are not supported,
* EINTR the function was interrupted by a signal,
* EDEADLK a deadlock condition was detected.
*
* ------------------------------------------------------
*/
{
int result = 0;
sem_t s = *sem;

if (s == NULL)
{
result = EINVAL;
}
else
{
if ((result = pthread_mutex_lock (&s->lock)) == 0)
{
int v = --s->value;

(void) pthread_mutex_unlock (&s->lock);

if (v < 0)
{
/* Must wait */
if (WaitForSingleObject (s->sem, INFINITE) == WAIT_OBJECT_0)
{
#ifdef NEED_SEM
if (pthread_mutex_lock (&s->lock) == 0)
{
if (s->leftToUnblock > 0)
{
--s->leftToUnblock;
SetEvent(s->sem);
}
(void) pthread_mutex_unlock (&s->lock);
}
#endif
return 0;
}
}
else
{
return 0;
}
}
}

if (result != 0)
{
errno = result;
return -1;
}

return 0;

} /* ptw32_semwait */
Loading

0 comments on commit c2c2749

Please sign in to comment.