瀏覽代碼

Store pointer in init_thread (See #534) (Step 3/3)

bel2125 6 年之前
父節點
當前提交
c32dad6cf3
共有 3 個文件被更改,包括 38 次插入1 次删除
  1. 4 1
      docs/api/mg_callbacks.md
  2. 2 0
      docs/api/mg_get_context.md
  3. 32 0
      docs/api/mg_get_thread_pointer.md

+ 4 - 1
docs/api/mg_callbacks.md

@@ -22,11 +22,14 @@
 | |The callback function `init_lua()` is called just before a Lua server page is to be served. Lua page serving must have been enabled at compile time for this callback function to be called. The parameter `lua_context` is a `lua_State *` pointer.|
 |**`init_ssl`**|**`int (*init_ssl)( void *ssl_context, void *user_data );`**|
 | |The callback function `init_ssl()` is called when CivetWeb initializes the SSL library. The parameter `user_data` contains a pointer to the data which was provided to `mg_start()` when the server was started. The callback function can return 0 to signal that CivetWeb should setup the SSL certificate. With a return value of 1 the callback function signals CivetWeb that the certificate has already been setup and no further processing is necessary. The value -1 should be returned when the SSL initialization fails.|
-|**`init_thread`**|**`void (*init_thread)( const struct mg_context *ctx, int thread_type );`**|
+|**`init_thread`**|**`void * (*init_thread)( const struct mg_context *ctx, int thread_type );`**|
 | |The callback function `init_thread()` is called when a new thread is created by CivetWeb. The `thread_type` parameter indicates which type of thread has been created. following thread types are recognized:|
 | |**0** - The master thread is created |
 | |**1** - A worker thread which handles client connections has been created|
 | |**2** - An internal helper thread (timer thread) has been created|
+| |The returned `void *` is stored as user defined pointer in the thread local storage.|
+|**`exit_thread`**|**`void (*exit_thread)( const struct mg_context *ctx, int thread_type, void * user_ptr);`**|
+| |The callback function `exit_thread()` is called when a thread is about to exit. The parameters correspond to `init_thread`, with `user_ptr` being the return value.|
 |**`log_access`**|**`int (*log_access)( const struct mg_connection *conn, const char *message );`**|
 | |The callback function `log_access()` is called when CivetWeb is about to log a message. If the callback function returns 0, CivetWeb will use the default internal access log routines to log the access. If a non-zero value is returned, CivetWeb assumes that access logging has already been done and no further action is performed.|
 |**`log_message`**|**`int (*log_message)( const struct mg_connection *conn, const char *message );`**|

+ 2 - 0
docs/api/mg_get_context.md

@@ -22,3 +22,5 @@ The function `mg_get_context()` returns the context associated with a connection
 
 * [`mg_start();`](mg_start.md)
 * [`mg_stop();`](mg_stop.md)
+* [`mg_get_user_data();`](mg_get_user_data.md)
+* [`mg_get_thread_pointer();`](mg_get_thread_pointer.md)

+ 32 - 0
docs/api/mg_get_thread_pointer.md

@@ -0,0 +1,32 @@
+# Civetweb API Reference
+
+### `mg_get_thread_pointer( conn );`
+
+### Parameters
+
+| Parameter | Type | Description |
+| :--- | :--- | :--- |
+|**`conn`**|`const struct mg_connection *`|The connection, for which the context has to be returned, or NULL|
+
+### Return Value
+
+| Type | Description |
+| :--- | :--- |
+|`void *`|A pointer to the context of the given connection|
+
+### Description
+
+The function `mg_get_thread_pointer()` returns the user defined pointer associated with the thread.
+This pointer is set as return value of the `mg_init_thread` callback (see [`struct mg_callbacks`](mg_callbacks.md)).
+
+The `conn` parameter can be NULL - in this case, the user defined pointer is taken from the thread local storage.
+For callbacks executed by server worker threads, `conn` can be the connection handle - in this case,
+the user defined pointer is taken from the connection handle.
+In both cases, the result will be the same pointer. Reading the pointer from the connection handle might be
+faster on some systems.
+
+### See Also
+
+* [`struct mg_callbacks`](mg_callbacks.md)
+* [`mg_get_user_data();`](mg_get_user_data.md)
+* [`mg_get_context();`](mg_get_context.md)