Skip to content

Commit

Permalink
Merge pull request #14 from SilvanScherrer/master
Browse files Browse the repository at this point in the history
add pthread_condattr_setclock() and friends
  • Loading branch information
dmik committed Sep 27, 2021
2 parents 4004138 + a755c87 commit ed15fbd
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Makefile.kmk
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ LIBRARIES = pthread_s pthread_g pthread_dll
pthread_s_SOURCES = \
pthread_attr.c \
pthread_sigmask.c \
pthread_condattr.c \
rwlock.c \
my_os2cond.c \
my_os2mutex.c \
Expand Down
29 changes: 25 additions & 4 deletions src/my_os2cond.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
{
APIRET rc = 0;
pthread_cond_t cv = NULL;
pthread_condattr_t cond_attr = NULL;

if (cond == NULL)
{
Expand All @@ -56,9 +57,21 @@ int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
return ENOMEM;
}

cv->attr = (pthread_condattr_t) calloc (1, sizeof (*cond_attr));
if (cv->attr == NULL)
{
return ENOMEM;
}

cv->waiting = -1;
cv->semaphore = -1;

// right now we only handle the clock_id attribute
if (attr == NULL || *attr == NULL)
cv->attr->clock_id = CLOCK_REALTIME;
else
cv->attr->clock_id = (*attr)->clock_id;

/* Warp3 FP29 or Warp4 FP4 or better required */
rc = DosCreateEventSem( NULL, (PHEV)&cv->semaphore, 0x0800, 0);
if (rc) {
Expand Down Expand Up @@ -97,6 +110,12 @@ int pthread_cond_destroy(pthread_cond_t *cond)
if (rc == ERROR_SEM_BUSY) DosPostEventSem(cv->semaphore);
} while (rc == ERROR_SEM_BUSY);

if (cv->attr)
{
(void) free(cv->attr);
cv->attr = NULL;
}

(void) free (cv);

if (rc)
Expand Down Expand Up @@ -146,10 +165,11 @@ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
return rval;
}


int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
struct timespec const *abstime)
{
struct timeb curtime;
struct timespec ts;
long timeout;
APIRET rc = 0;
int rval;
Expand All @@ -166,9 +186,10 @@ int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,

cv = *cond;

_ftime(&curtime);
timeout= ((long) (abstime->tv_sec - curtime.time)*1000L +
(long)((abstime->tv_nsec/1000) - curtime.millitm)/1000L);
clock_gettime (cv->attr->clock_id, &ts);
timeout= ((long) (abstime->tv_sec - ts.tv_sec)*1000L +
(long)((abstime->tv_nsec - ts.tv_nsec)/1000)/1000L);

if (timeout < 0) /* Some safety */
timeout = 0L;

Expand Down
4 changes: 4 additions & 0 deletions src/pthread.def
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ _pthread_rwlock_rdlock
_pthread_rwlock_trywrlock
_pthread_rwlock_wrlock
_pthread_sigmask
_pthread_condattr_init
_pthread_condattr_destroy
_pthread_condattr_setclock
_pthread_condattr_getclock
6 changes: 4 additions & 2 deletions src/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ int pthread_sigmask(int, const sigset_t *, sigset_t *);
/*
* PThread Attribute Functions
*/
#define pthread_condattr_init(A) 0
#define pthread_condattr_destroy(A) 0
int pthread_condattr_init(pthread_condattr_t *attr);
int pthread_condattr_destroy(pthread_condattr_t *attr);
int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id);
int pthread_condattr_getclock(pthread_condattr_t *attr, clockid_t clock_id);
#define pthread_attr_setscope(A,B)
int pthread_attr_init(pthread_attr_t *connect_att);
int pthread_attr_setprio(pthread_attr_t *connect_att,int priority);
Expand Down
126 changes: 126 additions & 0 deletions src/pthread_condattr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#define INCL_DOS
#define INCL_EXAPIS
#define INCL_EXAPIS_MAPPINGS
#include <os2emx.h>

#include <malloc.h>
#include <process.h>
#include <strings.h>
#include <sys/timeb.h>

#include "pthread.h"
#include "pthread_private.h"



int pthread_condattr_init(pthread_condattr_t *attr)
{
/*
* ------------------------------------------------------
* DOCPUBLIC
* Initializes a condition variable attributes object
* with default attributes.
*
* PARAMETERS
* attr
* pointer to an instance of pthread_condattr_t
*
* DESCRIPTION
* Initializes a condition variable attributes object
* with default attributes.
*
* NOTES:
* 1) Use to define condition variable types
* 2) It is up to the application to ensure
* that it doesn't re-init an attribute
* without destroying it first. Otherwise
* a memory leak is created.
*
* RESULTS
* 0 successfully initialized attr,
* ENOMEM insufficient memory for attr.
*
* ------------------------------------------------------
*/
pthread_condattr_t attr_result;
int result = 0;

attr_result = (pthread_condattr_t) calloc(1, sizeof(*attr_result));

if (attr_result == NULL)
{
return ENOMEM;
}

*attr = attr_result;

return result;
}

int pthread_condattr_destroy(pthread_condattr_t *attr)
{
/*
* ------------------------------------------------------
* DOCPUBLIC
* Destroys a condition variable attributes object
* The object can no longer be used.
*
* PARAMETERS
* attr
* pointer to an instance of pthread_condattr_t
*
* DESCRIPTION
* Destroys a condition variable attributes object
* The object can no longer be used.
*
* NOTES:
* 1) Does not affect condition variables created
* using 'attr'
*
* RESULTS
* 0 successfully released attr,
* EINVAL 'attr' is invalid.
*
* ------------------------------------------------------
*/
int result = 0;

if (attr == NULL || *attr == NULL)
{
result = EINVAL;
}
else
{
(void) free(*attr);
*attr = NULL;
result = 0;
}

return result;
}

int pthread_condattr_getclock(pthread_condattr_t *attr, clockid_t clock_id)
{
int result = 0;

if (attr == NULL || *attr == NULL)
{
result = EINVAL;
}

clock_id = (*attr)->clock_id;

return result;
}

int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id)
{
int result = 0;

if (clock_id != CLOCK_MONOTONIC && clock_id != CLOCK_REALTIME)
result = EINVAL;

(*attr)->clock_id = clock_id;

return result;
}
7 changes: 4 additions & 3 deletions src/pthread_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@ struct pthread_t_
struct pthread_attr_t_ {
uint32_t dwStackSize ;
uint32_t dwCreatingFlag ;
int priority ;
int detachstate;
int priority ;
int detachstate;
};

struct pthread_condattr_t_ {
int dummy;
clockid_t clock_id; // CLOCK_MONOTONIC or CLOCK_REALTIME
};

struct pthread_cond_t_ {
int waiting;
uint32_t semaphore;
pthread_condattr_t attr;
};

struct pthread_mutex_t_
Expand Down

0 comments on commit ed15fbd

Please sign in to comment.