Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Osx portability #1

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/common/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
#define _GNU_SOURCE
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#endif

#include "debug.h"

#ifdef _WIN32
int TWI_Get_tid (void) { return (int)GetThreadId (); }
#else
int TWI_Get_tid (void) { return (int)gettid (); }
int TWI_Get_tid (void) { return (int)pthread_self (); }
#endif

int TWI_Debug_level = 0;
int TWI_Debug_level = 0;
4 changes: 2 additions & 2 deletions src/tal/posix/twposix.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#pragma once

#include <error.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>

Expand Down Expand Up @@ -62,4 +62,4 @@ terr_t TWPOSIX_Join (TW_handle_t ht, void **ret);
terr_t TWPOSIX_Cancel (TW_handle_t ht);
void TWPOSIX_Exit (void *ret);

terr_t TWPOSIX_Err_to_tw_err (int TWI_UNUSED perr);
terr_t TWPOSIX_Err_to_tw_err (int TWI_UNUSED perr);
29 changes: 19 additions & 10 deletions src/tal/posix/twposix_sem.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* Pthread driver implementation */

#include <sys/fcntl.h>
#include "twposix_sem.h"

#include <semaphore.h>

sem_t *make_semaphore(int value);
terr_t TWPOSIX_Sem_create (TW_Handle_t *sem) {
terr_t err = TW_SUCCESS;
int perr;
int perr = TW_SUCCESS;
sem_t *sp = NULL;

sp = (sem_t *)TWI_Malloc (sizeof (sem_t));

perr = sem_init (sp, 0, 0);
sp = make_semaphore(0);
if(sp == SEM_FAILED)
perr = TW_ERR_OS;
CHECK_PERR

*sem = sp;
Expand All @@ -33,7 +34,7 @@ err_out:;

void TWPOSIX_Sem_trydec (TW_Handle_t sem, TWI_Bool_t *success) {
int perr;
perr = sem_trywait (sem);
perr = sem_wait (sem);
if (perr == 0) {
*success = TWI_TRUE;
} else {
Expand All @@ -45,7 +46,15 @@ void TWPOSIX_Sem_dec (TW_Handle_t sem) { sem_wait (sem); }

void TWPOSIX_Sem_inc (TW_Handle_t sem) { sem_post (sem); }

void TWPOSIX_Sem_free (TW_Handle_t sem) {
sem_destroy (sem);
TWI_Free (sem);
}
void TWPOSIX_Sem_free (TW_Handle_t sem) { sem_close(sem); }

sem_t *make_semaphore(int value){
sem_t *semaphore = (sem_t *) malloc(sizeof(sem_t));

char sem_name[128] = {};
sprintf(sem_name, "sem-%d", pthread_self());
semaphore = sem_open(sem_name, O_CREAT, 0644, 0);
sem_unlink(sem_name);

return semaphore;
}
27 changes: 19 additions & 8 deletions test/common/semaphore.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
terr_t TWT_Sem_create (TWT_Semaphore *sem) {
int ret;
TWT_Semaphore s;

s = (TWT_Semaphore)malloc (sizeof (sem_t));

ret = sem_init (s, 0, 0);
char sem_name[128] = {};
sprintf(sem_name, "sem-%d", pthread_self());

s = sem_open(sem_name, O_CREAT, 0644, 0);
sem_unlink(sem_name);

if(s == SEM_FAILED) ret = -1;
else ret = 0;

if (ret != 0) return TW_ERR_OS;

*sem = s;
Expand Down Expand Up @@ -34,11 +41,15 @@ terr_t TWT_Sem_inc (TWT_Semaphore sem) {

terr_t TWT_Sem_free (TWT_Semaphore sem) {
int ret;

ret = sem_destroy (sem);
if (ret != 0) return TW_ERR_OS;

free (sem);
ret = sem_close(sem);
if (ret != 0)
return TW_ERR_OS;

return TW_SUCCESS;
}
}

void SEMAPHORE_BUG_WARNING(){
#ifdef __APPLE__
assert(0 && "Taskworks has a known bug with semaphore on OSX that may lead to a deadlock, not fixed yet.");
#endif
}
7 changes: 5 additions & 2 deletions test/common/twtest.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <sys/fcntl.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>

#include <errno.h>
#include <assert.h>
#ifdef _WIN32
#else
#endif
Expand Down Expand Up @@ -66,4 +68,5 @@ typedef sem_t *TWT_Semaphore;
terr_t TWT_Sem_create (TWT_Semaphore *sem);
terr_t TWT_Sem_dec (TWT_Semaphore sem);
terr_t TWT_Sem_inc (TWT_Semaphore sem);
terr_t TWT_Sem_free (TWT_Semaphore sem);
terr_t TWT_Sem_free (TWT_Semaphore sem);
void SEMAPHORE_BUG_WARNING();
5 changes: 2 additions & 3 deletions test/event/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ LDADD = $(top_builddir)/src/libtw.la \

TESTPROGRAMS = init \
engine \
file \
timer \
socket \
poll

poll \
file
if HAVE_MPI
TESTPROGRAMS += mpi \
mpi_req
Expand Down
6 changes: 4 additions & 2 deletions test/event/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ int main (int argc, char **argv) {
atomic_int evtnerr = 0;
TW_Fd_t fd;
char msg[] = "test_msg";
char cmd[256];
char cmd[256] = {0};
TW_Event_args_t arg;
TW_Event_handle_t evt;
TW_Engine_handle_t eng;

SEMAPHORE_BUG_WARNING();

PRINT_TEST_MSG ("Check if file event triggers correctly");

if (argc > 1) { nworker = atoi (argv[1]); }
Expand Down Expand Up @@ -181,7 +183,7 @@ int main (int argc, char **argv) {

// echo will add \n after the end of the message
sprintf (cmd, "echo \"%s\" >> file.txt", msg);
system (cmd);
int r = system (cmd);

err = TWT_Sem_dec (sem);
CHECK_ERR
Expand Down
4 changes: 2 additions & 2 deletions test/task/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ TESTPROGRAMS = init \
task \
task_dep \
task_barrier \
custom_dep \
task_status
task_status \
custom_dep
# mutual_dep

TEST_DRIVERS= "NATIVE"
Expand Down
13 changes: 10 additions & 3 deletions test/task/custom_dep.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ int Custom_dep_status_change (TW_Task_handle_t task,

// Try CAS in my own handle if available
if (h == TW_HANDLE_NULL || (parent == h && new_status == TW_TASK_STAT_COMPLETED)) {
if (atomic_compare_exchange_strong (hp, &h, task)) { return TW_TASK_STAT_READY; }
#ifdef __APPLE__
if (atomic_compare_exchange_strong ((_Atomic*)hp, &h, task)) { return TW_TASK_STAT_READY; }
#else
if (atomic_compare_exchange_strong (hp, &h, task)) { return TW_TASK_STAT_READY; }
#endif

}

return TW_TASK_STAT_DEPHOLD;
Expand All @@ -75,7 +80,9 @@ int main (int argc, char *argv[]) {
TW_Task_dep_handler_t dep;
TW_Task_handle_t *task = NULL;
task_data *datas = NULL;


SEMAPHORE_BUG_WARNING();

PRINT_TEST_MSG ("Check if Taskworks correctly handles customized dependency");

if (argc > 1) { nworker = atoi (argv[1]); }
Expand Down Expand Up @@ -149,4 +156,4 @@ int main (int argc, char *argv[]) {

PRINT_TEST_RESULT
return nerr;
}
}
2 changes: 1 addition & 1 deletion test/task/task_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ int main (int argc, char *argv[]) {

PRINT_TEST_RESULT
return nerr;
}
}