-
Notifications
You must be signed in to change notification settings - Fork 37
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
add thread:setname and thread:getname #208
Open
slact
wants to merge
4
commits into
wahern:master
Choose a base branch
from
slact:thread-setname
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,9 @@ | |
#include <sys/socket.h> | ||
|
||
#include <pthread.h> | ||
#if (HAVE_OPENBSD_PTHREAD || HAVE_FREEBSD_PTHREAD) | ||
#include <pthread_np.h> | ||
#endif | ||
|
||
#include <dlfcn.h> | ||
|
||
|
@@ -755,6 +758,97 @@ static int ct_timeout(lua_State *L) { | |
return 0; | ||
} /* ct_timeout() */ | ||
|
||
#if HAVE_PTHREAD_SETNAME | ||
static int ct_setname(lua_State *L) { | ||
struct cthread *ct = ct_checkthread(L, 1); | ||
char *name = luaL_checkstring(L, 2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is too much whitespace here. |
||
int rc; | ||
#if HAVE_GLIBC_PTHREAD | ||
rc = pthread_setname_np(ct->id, name); | ||
#elif HAVE_NETBSD_PTHREAD | ||
rc = pthread_setname_np(ct->id, "%s", name); | ||
#elif (HAVE_FREEBSD_PTHREAD || HAVE_OPENBSD_PTHREAD) | ||
rc = 0; | ||
/* from FreeBSD & OpenBSD man page: | ||
Because of the debugging nature of this function, all errors that may | ||
appear inside are silently ignored. | ||
*/ | ||
pthread_set_name_np(ct->id, name); | ||
#elif HAVE_MACOSX_PTHREAD | ||
if (pthread_equal(ct->id, pthread_self())) { | ||
lua_pushboolean(L, 0); | ||
lua_pushliteral(L, "thread name cannot be set from outside the thread on this platform"); | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
lua_pushinteger(L, EPERM); | ||
return 3; | ||
} | ||
rc = pthread_setname_np(name); | ||
#endif | ||
switch(rc) { | ||
case 0: | ||
lua_pushboolean(L, 1); | ||
return 1; | ||
case ERANGE: | ||
lua_pushboolean(L, 0); | ||
lua_pushliteral(L, "thread name too long"); | ||
lua_pushinteger(L, rc); | ||
return 3; | ||
case EINVAL: | ||
lua_pushboolean(L, 0); | ||
lua_pushliteral(L, "invalid parameter when setting thread name"); | ||
lua_pushinteger(L, rc); | ||
return 3; | ||
case ENOMEM: | ||
lua_pushboolean(L, 0); | ||
lua_pushliteral(L, "out of memory when setting thread name"); | ||
lua_pushinteger(L, rc); | ||
return 3; | ||
} | ||
} /* ct_setname() */ | ||
#else | ||
static int ct_setname(lua_State *L) { | ||
lua_pushnil(L); | ||
lua_pushliteral(L, "setname() not supported on this platform"); | ||
lua_pushinteger(L, EPERM); | ||
return 3; | ||
} /* ct_setname() */ | ||
#endif | ||
|
||
|
||
#if HAVE_PTHREAD_GETNAME | ||
static int ct_getname(lua_State *L) { | ||
struct cthread *ct = ct_checkthread(L, 1); | ||
char buf[128]; | ||
int rc = EINVAL; | ||
/*all the pthread_getname_np interfaces are the same where supported*/ | ||
rc = pthread_getname_np(ct->id, buf, 128); | ||
switch(rc) { | ||
case ERANGE: | ||
lua_pushnil(L); | ||
lua_pushliteral(L, "thread name too long"); | ||
lua_pushinteger(L, rc); | ||
return 3; | ||
case EINVAL: | ||
lua_pushnil(L); | ||
lua_pushliteral(L, "invalid parameter when getting thread name"); | ||
lua_pushinteger(L, rc); | ||
return 3; | ||
case ENOMEM: | ||
lua_pushnil(L); | ||
lua_pushliteral(L, "out of memory when getting thread name"); | ||
lua_pushinteger(L, rc); | ||
return 3; | ||
} | ||
lua_pushstring(L, buf); | ||
return 1; | ||
} /* ct_getname() */ | ||
#else | ||
static int ct_getname(lua_State *L) { | ||
lua_pushnil(L); | ||
lua_pushliteral(L, "getname() not supported on this platform"); | ||
lua_pushinteger(L, EPERM); | ||
return 3; | ||
} /* ct_getname() */ | ||
#endif | ||
|
||
static int ct__eq(lua_State *L) { | ||
struct cthread **a = luaL_testudata(L, 1, CQS_THREAD); | ||
|
@@ -804,6 +898,8 @@ static const luaL_Reg ct_methods[] = { | |
{ "pollfd", &ct_pollfd }, | ||
{ "events", &ct_events }, | ||
{ "timeout", &ct_timeout }, | ||
{ "setname", &ct_setname }, | ||
{ "getname", &ct_getname }, | ||
{ NULL, NULL } | ||
}; | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The define is only really about pthread_setname, please rename them all accordingly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Different pthread implementations have different function signatures, and some only have
setname
and nogetname
. That's why I've introduced theHAVE_<PLATFORM>_PTHREAD
macro as welll asHAVE_PTHREAD_SETNAME
andHAVE_PTHREAD_GETNAME
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that really makes sense though: the values you've chosen here are very geared to the setname call.
e.g. glibc had pthreads long before 2.12.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true -- i'll use some other name then