Skip to content

Commit

Permalink
Toughen qv_pthread* interfaces. (#219)
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel K. Gutierrez <[email protected]>
  • Loading branch information
samuelkgutierrez authored Jul 12, 2024
1 parent ac1e971 commit 32fd2c9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion include/quo-vadis-pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ qv_pthread_routine(
int
qv_pthread_create(
pthread_t *thread,
pthread_attr_t *attr,
const pthread_attr_t *attr,
void *(*thread_routine)(void *arg),
void *arg,
qv_scope_t *scope
Expand Down
67 changes: 42 additions & 25 deletions src/quo-vadis-pthread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@ qv_pthread_scope_split(
int nthreads,
qv_scope_t ***subscope
) {
return qvi_scope_ksplit(
scope, npieces, color_array, nthreads, QV_HW_OBJ_LAST, subscope
);
if (!scope || npieces < 0 || !color_array || nthreads < 0 || !subscope) {
return QV_ERR_INVLD_ARG;
}
try {
return qvi_scope_ksplit(
scope, npieces, color_array, nthreads,
QV_HW_OBJ_LAST, subscope
);
}
qvi_catch_and_return();
}

int
Expand All @@ -43,9 +50,15 @@ qv_pthread_scope_split_at(
int nthreads,
qv_scope_t ***subscopes
) {
return qvi_scope_ksplit_at(
scope, type, color_array, nthreads, subscopes
);
if (!scope || !color_array || nthreads < 0 || !subscopes) {
return QV_ERR_INVLD_ARG;
}
try {
return qvi_scope_ksplit_at(
scope, type, color_array, nthreads, subscopes
);
}
qvi_catch_and_return();
}

void *
Expand All @@ -56,30 +69,31 @@ qv_pthread_routine(
qvi_scope_bind_push(arg_ptr->scope);

void *ret = arg_ptr->thread_routine(arg_ptr->arg);
// Free memory allocated in qv_pthread_create
// TODO(skg) Should we consider adding a free call-back? Are there any data
// here that may require user-level control?
delete arg_ptr;
qvi_delete(&arg_ptr);
pthread_exit(ret);
}

/**
* Similar to pthread_create(3).
*/
int
qv_pthread_create(
pthread_t *thread,
pthread_attr_t *attr,
void *(*thread_routine)(void *arg),
void *arg,
qv_scope_t *scope
pthread_t *thread,
const pthread_attr_t *attr,
void *(*thread_routine)(void *arg),
void *arg,
qv_scope_t *scope
) {
// Memory will be freed in qv_pthread_routine to avoid memory leaks.
qv_pthread_args_t *arg_ptr = nullptr;
int rc = qvi_new(&arg_ptr);
if (rc != QV_SUCCESS) return rc;

arg_ptr->scope = scope;
arg_ptr->thread_routine = thread_routine;
arg_ptr->arg = arg;
// Memory will be freed in qv_pthread_routine to avoid memory leaks.
qv_pthread_args_t *arg_ptr = nullptr;
const int rc = qvi_new(&arg_ptr);
// Since this is meant to behave similarly to
// pthread_create(), return a reasonable errno.
if (rc != QV_SUCCESS) return ENOMEM;

arg_ptr->scope = scope;
arg_ptr->thread_routine = thread_routine;
arg_ptr->arg = arg;
return pthread_create(thread, attr, qv_pthread_routine, arg_ptr);
}

Expand All @@ -89,8 +103,11 @@ qv_pthread_scope_free(
qv_scope_t **scopes
) {
if (nscopes < 0 || !scopes) return QV_ERR_INVLD_ARG;
qvi_scope_kfree(&scopes, nscopes);
return QV_SUCCESS;
try {
qvi_scope_kfree(&scopes, nscopes);
return QV_SUCCESS;
}
qvi_catch_and_return();
}

/*
Expand Down

0 comments on commit 32fd2c9

Please sign in to comment.