Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8f89a5c

Browse filesBrowse files
committed
stl_thread_add adds an existing thread to the thread table, use it for main thread and web server thread
this helps the display with stl_log add printf attribute for log and error functions
1 parent b3f7cfd commit 8f89a5c
Copy full SHA for 8f89a5c

File tree

3 files changed

+55
-8
lines changed
Filter options

3 files changed

+55
-8
lines changed

‎stl/httpd.c

Copy file name to clipboardExpand all lines: stl/httpd.c
+12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ static char *req_method;
3636

3737
int web_debug_flag = 1;
3838

39+
pthread_t web_pthread = NULL;
40+
int page_request_once = 0;
41+
3942
static int
4043
page_request (void *cls, struct MHD_Connection *connection,
4144
const char *url, const char *method,
@@ -48,6 +51,11 @@ MHD_get_connection_values (connection, MHD_HEADER_KIND, print_key, NULL);
4851
MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND, print_key, NULL);
4952
*/
5053

54+
if (page_request_once == 0) {
55+
page_request_once = 1;
56+
stl_thread_add("WEB");
57+
}
58+
5159
WEB_DEBUG("web: %s request for URL %s using %s", method, url, version);
5260

5361
// save some of the parameters for access by MATLAB calls
@@ -205,6 +213,10 @@ web_start(int32_t port, char *callback)
205213

206214
daemon = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD, port, NULL, NULL,
207215
&page_request, NULL, MHD_OPTION_END);
216+
217+
// this starts a POSIX thread but its handle is very well buried
218+
// its name will be MHD-single but this is not gettable under MacOS
219+
208220
if (daemon == NULL)
209221
stl_error("web server failed to launch: %s", strerror(errno));
210222

‎stl/stl.c

Copy file name to clipboardExpand all lines: stl/stl.c
+40-6
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,7 @@ stl_initialize(int argc, char **argv)
113113
stl_error("initialize: mutex create failed %s", strerror(status));
114114

115115
// allocate a dummy thread list entry for the main thread
116-
threadlist[0].busy = 1;
117-
threadlist[0].name = "user";
118-
threadlist[0].f = NULL; // no pointer to function entry
119-
threadlist[0].pthread = (pthread_t)NULL; // it has no thread handle
116+
stl_thread_add("user");
120117
}
121118

122119
void
@@ -199,10 +196,47 @@ stl_thread_create(char *func, void *arg, char *name)
199196
return slot;
200197
}
201198

199+
int
200+
stl_thread_add(char *name)
201+
{
202+
int slot;
203+
thread *p, *tp = NULL;
204+
205+
// find an empty slot
206+
LIST_LOCK
207+
for (p=threadlist, slot=0; slot<NTHREADS; slot++, p++) {
208+
if (p->busy == 0) {
209+
tp = p;
210+
tp->busy++; // mark it busy
211+
break;
212+
}
213+
}
214+
LIST_UNLOCK
215+
if (tp == NULL)
216+
stl_error("too many threads, increase NTHREADS (currently %d)", NTHREADS);
217+
218+
tp->name = stl_stralloc(name);
219+
tp->pthread = pthread_self();
220+
tp->f = NULL;
221+
222+
return slot;
223+
}
224+
225+
202226
static void
203227
stl_thread_wrapper( thread *tp)
204228
{
205229
STL_DEBUG("starting posix thread <%s> (0x%X)", tp->name, (uint32_t)tp->f);
230+
231+
// tell kernel the thread's name
232+
// under linux can see this with ps -o cat /proc/$PID/task/$TID/comm
233+
// settable for MacOS but seemingly not visible
234+
#ifdef __linux__ || __unix__
235+
pthread_setname_np(tp->pthread, tp->name);
236+
#endif
237+
#ifdef __APPLE__
238+
pthread_setname_np(tp->name);
239+
#endif
206240

207241
// invoke the user's compiled MATLAB code
208242
tp->f(tp->arg);
@@ -215,9 +249,9 @@ stl_thread_wrapper( thread *tp)
215249
char *
216250
stl_thread_name(int32_t slot)
217251
{
218-
if (slot < 0)
252+
if (slot < 0)
219253
slot = stl_thread_self();
220-
254+
221255
return threadlist[slot].name;
222256
}
223257

‎stl/stl.h

Copy file name to clipboardExpand all lines: stl/stl.h
+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
// function signatures
44
void stl_initialize(int argc, char **argv);
5-
void stl_log(const char *fmt, ...);
6-
void stl_error(const char *fmt, ...);
5+
void stl_log(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
6+
void stl_error(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
77
void stl_debug(int32_t debug);
88
void *stl_get_functionptr(char *name);
99
char *stl_stralloc(char *s);
@@ -17,6 +17,7 @@ int32_t stl_thread_join(int32_t slot);
1717
void stl_thread_cancel(int32_t slot);
1818
int32_t stl_thread_self();
1919
char * stl_thread_name(int32_t id);
20+
int stl_thread_add(char *name);
2021

2122
// command line arguments
2223
int32_t stl_argc();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.