Skip to content

Commit

Permalink
Fix my ignoring of fadvise, make pthread operations have a safety che…
Browse files Browse the repository at this point in the history
…ck to ensure that they are initialized prior to use, and make condattr "work" but cond_init fail if you use invalid ones.
  • Loading branch information
Filip Pizlo committed Apr 23, 2024
1 parent 164bc69 commit e753698
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions include/alltypes.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ TYPEDEF unsigned socklen_t;
TYPEDEF unsigned short sa_family_t;

TYPEDEF struct { union { int __i[sizeof(long)==8?14:9]; volatile int __vi[sizeof(long)==8?14:9]; unsigned long __s[sizeof(long)==8?7:9]; } __u; } pthread_attr_t;
TYPEDEF struct { int __i; void* __p } pthread_mutex_t;
TYPEDEF struct { int __i; int __inited; void* __p } pthread_mutex_t;
TYPEDEF struct { union { int __i[sizeof(long)==8?10:6]; volatile int __vi[sizeof(long)==8?10:6]; volatile void *volatile __p[sizeof(long)==8?5:6]; } __u; } mtx_t;
TYPEDEF struct { int __i; void *__p; } pthread_cond_t;
TYPEDEF struct { int __i; int __inited; void *__p; } pthread_cond_t;
TYPEDEF struct { union { int __i[12]; volatile int __vi[12]; void *__p[12*sizeof(int)/sizeof(void*)]; } __u; } cnd_t;
TYPEDEF struct { int __i; void *__p; } pthread_rwlock_t;
TYPEDEF struct { union { int __i[sizeof(long)==8?8:5]; volatile int __vi[sizeof(long)==8?8:5]; void *__p[sizeof(long)==8?4:5]; } __u; } pthread_barrier_t;
Expand Down
1 change: 1 addition & 0 deletions src/fcntl/posix_fadvise.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

int posix_fadvise(int fd, off_t base, off_t len, int advice)
{
return 0;
}
1 change: 1 addition & 0 deletions src/thread/pthread_cond_broadcast.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

int pthread_cond_broadcast(pthread_cond_t *c)
{
ZASSERT(c->__inited == 42);
if (!c->__i)
return 0;
c->__i = 0;
Expand Down
1 change: 1 addition & 0 deletions src/thread/pthread_cond_destroy.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

int pthread_cond_destroy(pthread_cond_t *c)
{
ZASSERT(c->__inited == 42);
return 0;
}
12 changes: 11 additions & 1 deletion src/thread/pthread_cond_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@

int pthread_cond_init(pthread_cond_t *restrict c, const pthread_condattr_t *restrict a)
{
ZASSERT(!a);
if (a) {
clockid_t clock;
ZASSERT(!pthread_condattr_getclock(a, &clock));
if (clock != CLOCK_REALTIME)
return ENOSYS;
int pshared;
ZASSERT(!pthread_condattr_getpshared(a, &pshared));
if (pshared)
return ENOSYS;
}
c->__i = 0;
c->__inited = 42;
return 0;
}
1 change: 1 addition & 0 deletions src/thread/pthread_cond_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ static void callback(_Bool did_unpark_thread, _Bool may_have_more_threads, void*

int pthread_cond_signal(pthread_cond_t *c)
{
ZASSERT(c->__inited == 42);
if (!c->__i)
return 0;
zunpark_one(&c->__i, callback, c);
Expand Down
1 change: 1 addition & 0 deletions src/thread/pthread_cond_timedwait.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void before_sleep_callback(void* arg)

int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts)
{
ZASSERT(c->__inited == 42);
wait_data data;
data.c = c;
data.m = m;
Expand Down
1 change: 1 addition & 0 deletions src/thread/pthread_mutex_destroy.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
ZASSERT(mutex->__inited == 42);
return 0;
}
1 change: 1 addition & 0 deletions src/thread/pthread_mutex_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ int pthread_mutex_init(pthread_mutex_t *restrict m, const pthread_mutexattr_t *r
{
ZASSERT(!a);
m->__i = 0;
m->__inited = 42;
return 0;
}
1 change: 1 addition & 0 deletions src/thread/pthread_mutex_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

int __pthread_mutex_lock(pthread_mutex_t *m)
{
ZASSERT(m->__inited == 42);
volatile int* l = (volatile int*)&m->__i;
LOCK(l);
return 0;
Expand Down
1 change: 1 addition & 0 deletions src/thread/pthread_mutex_trylock.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

int __pthread_mutex_trylock(pthread_mutex_t *m)
{
ZASSERT(m->__inited == 42);
volatile int* l = (volatile int*)&m->__i;
TRYLOCK(l);
return 0;
Expand Down
1 change: 1 addition & 0 deletions src/thread/pthread_mutex_unlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

int __pthread_mutex_unlock(pthread_mutex_t *m)
{
ZASSERT(m->__inited == 42);
volatile int* l = (volatile int*)&m->__i;
UNLOCK(l);
return 0;
Expand Down

0 comments on commit e753698

Please sign in to comment.