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

add thread:setname and thread:getname #208

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions doc/cqueues.tex
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,14 @@ \subsubsection{\fn{signal[]}}

Returns a boolean and error value. If false, error value is an error code describing a local error, usually \errno{EAGAIN} or \errno{ETIMEDOUT}. If true, error value is 1) an error code describing a system error which the thread encountered, 2) an error message string returned by the new Lua instance, or 3) nil if completed successfully.

\subsubsection[\fn{thread:setname}]{\fn{thread:setname(name)}}
Set a name for the thread. The name must be a string not exceeding 15 characters in length.

Returns true on success, false and an error if the name is too long.

\subsubsection[\fn{thread:getname}]{\fn{thread:getname(name)}}
Get the name of the thread. Returns the name string.

\end{Module}

\begin{Module}{cqueues.notify}
Expand Down
27 changes: 27 additions & 0 deletions src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,31 @@ static int ct_timeout(lua_State *L) {
return 0;
} /* ct_timeout() */

static int ct_setname(lua_State *L) {
struct cthread *ct = ct_checkthread(L, 1);
char *name = luaL_checkstring(L, 2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is too much whitespace here.

int rc = pthread_setname_np(ct->id, name);
if(rc == ERANGE) {
lua_pushboolean(L, 0);
lua_pushliteral(L, "thread name too long");
return 2;
}
lua_pushboolean(L, 1);
return 1;
} /* ct_setname() */

static int ct_getname(lua_State *L) {
struct cthread *ct = ct_checkthread(L, 1);
char buf[64];
int rc = pthread_getname_np(ct->id, buf, 64);
if(rc == ERANGE) {
lua_pushboolean(L, 0);
lua_pushliteral(L, "thread name too long");
return 2;
}
lua_pushstring(L, buf);
return 1;
} /* ct_setname() */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't correct


static int ct__eq(lua_State *L) {
struct cthread **a = luaL_testudata(L, 1, CQS_THREAD);
Expand Down Expand Up @@ -804,6 +829,8 @@ static const luaL_Reg ct_methods[] = {
{ "pollfd", &ct_pollfd },
{ "events", &ct_events },
{ "timeout", &ct_timeout },
{ "setname", &ct_setname },
{ "getname", &ct_getname },
{ NULL, NULL }
};

Expand Down