Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
The Lua Thread Library

Detailed Description

See Advanced Lua Bindings, section Calling Lua Code Asynchronously From C Code for how to use this class.

Collaboration diagram for The Lua Thread Library:

Classes

struct  LThreadMgr
 The global instance created by C code or a dynamic instance created by ba.thread.create More...
 
struct  ThreadJob
 A thread job created by ThreadJob_create or ThreadJob_lcreate. More...
 

Macros

#define LThreadMgr_canRun(o)   ! DoubleList_isEmpty(&(o)->idleThreadList)
 This function returns true if at least one thread is currently idle and there is no need to queue the job when calling LThreadMgr_run. More...
 
#define LThreadMgr_enableHttpPool(o, server)    HttpServer_setThreadPoolIntf(server, (HttpCmdThreadPoolIntf*)o)
 LThreadMgr can be used as a Thread Pool and is often utilized in real-time operating system (RTOS) devices with limited resources as a substitute for HttpCmdThreadPool. More...
 

Typedefs

typedef void(* ThreadJob_Run) (struct ThreadJob *tj, struct LThreadMgr *mgr)
 Generic ThreadJob callback. More...
 
typedef void(* ThreadJob_LRun) (struct ThreadJob *tj, int msgh, struct LThreadMgr *mgr)
 ThreadJob callback designed for calling Lua code using lua_pcall. More...
 
typedef struct LThreadMgr LThreadMgr
 The global instance created by C code or a dynamic instance created by ba.thread.create More...
 
typedef struct ThreadJob ThreadJob
 A thread job created by ThreadJob_create or ThreadJob_lcreate. More...
 

Functions

BA_API void LThreadMgr_destructor (LThreadMgr *o)
 This function terminates the Thread Pool and waits for all threads to stop before returning. More...
 
BA_API void LThreadMgr_constructor (LThreadMgr *o, HttpServer *server, ThreadPriority priority, int stackSize, int threads, lua_State *L, int allowCreate)
 Initialize the thread pool and install ba.thread. More...
 
BA_API ThreadJob * ThreadJob_create (size_t size, ThreadJob_Run run)
 Create a generic thread job. More...
 
BA_API ThreadJob * ThreadJob_lcreate (size_t size, ThreadJob_LRun lrun)
 Create a thread job designed to execute Lua code. More...
 
BA_API int LThreadMgr_run (LThreadMgr *o, ThreadJob *tj)
 This function sends a thread job to an available idle thread, or queues the job if no threads are currently available. More...
 

Macro Definition Documentation

◆ LThreadMgr_canRun

#define LThreadMgr_canRun (   o)    ! DoubleList_isEmpty(&(o)->idleThreadList)

This function returns true if at least one thread is currently idle and there is no need to queue the job when calling LThreadMgr_run.

Parameters
oRequired initialized manager; its server mutex must be held.
Returns
Nonzero if a worker is idle, zero otherwise. This is not a job result.

◆ LThreadMgr_enableHttpPool

#define LThreadMgr_enableHttpPool (   o,
  server 
)     HttpServer_setThreadPoolIntf(server, (HttpCmdThreadPoolIntf*)o)

LThreadMgr can be used as a Thread Pool and is often utilized in real-time operating system (RTOS) devices with limited resources as a substitute for HttpCmdThreadPool.

Parameters
othe LThreadMgr instance
serverthe HttpServer instance

Typedef Documentation

◆ LThreadMgr

typedef struct LThreadMgr LThreadMgr

The global instance created by C code or a dynamic instance created by ba.thread.create

◆ ThreadJob

typedef struct ThreadJob ThreadJob

A thread job created by ThreadJob_create or ThreadJob_lcreate.

◆ ThreadJob_LRun

typedef void(* ThreadJob_LRun) (struct ThreadJob *tj, int msgh, struct LThreadMgr *mgr)

ThreadJob callback designed for calling Lua code using lua_pcall.

The server mutex is held. Use tj->Lt and pass msgh to lua_pcall. The manager frees the job and clears the thread stack after return. Do not free/requeue the job, retain tj->Lt, or allow an unprotected Lua error to escape.

Parameters
tjthe job passed into function LThreadMgr_run
msghthe index position to the BAS error handler function
mgrthe manager associated with the global or dynamically created instance via ba.thread.create

◆ ThreadJob_Run

typedef void(* ThreadJob_Run) (struct ThreadJob *tj, struct LThreadMgr *mgr)

Generic ThreadJob callback.

The server mutex is held. The manager frees the job after return and clears its Lua stack. Do not free or requeue the job or retain its thread state. Release the mutex for blocking external work and reacquire it before return.

Parameters
tjthe job passed into function LThreadMgr_run
mgrthe manager associated with the global or dynamically created instance via ba.thread.create

Function Documentation

◆ LThreadMgr_constructor()

BA_API void LThreadMgr_constructor ( LThreadMgr *  o,
HttpServer *  server,
ThreadPriority  priority,
int  stackSize,
int  threads,
lua_State *  L,
int  allowCreate 
)

Initialize the thread pool and install ba.thread.

Call with the server mutex held. The server and Lua state are borrowed and must outlive the manager. Lua allocation errors and native thread-creation failures use the underlying Lua/platform error handling, not a return code.

Parameters
oThe LThreadMgr instance to initialize.
serverThe HttpServer instance that owns the pool.
prioritytypically set to ThreadPrioNormal
stackSizetypically set to BA_STACKSZ
threadsPositive initial worker count; zero provides no workers.
Lthe Lua state returned by balua_create
allowCreateset to TRUE to enable the two APIs ba.thread.create and ba.thread.configure

◆ LThreadMgr_destructor()

BA_API void LThreadMgr_destructor ( LThreadMgr *  o)

This function terminates the Thread Pool and waits for all threads to stop before returning.

Pending jobs may be discarded without callbacks.

Parameters
oRequired initialized manager. Stop submitting jobs first. Its server mutex must be held; it is released and reacquired while waiting. Keep the server and Lua VM alive until this returns, and do not call from its worker.

◆ LThreadMgr_run()

BA_API int LThreadMgr_run ( LThreadMgr *  o,
ThreadJob *  tj 
)

This function sends a thread job to an available idle thread, or queues the job if no threads are currently available.

Before calling this method from outside of the server environment, you must have ownership of the SoDisp ThreadMutex.

Parameters
oRequired running manager with at least one worker.
tjRequired allocated job, not already queued. Ownership transfers.
Returns
TRUE if an idle worker was signalled, FALSE if only queued. Both results mean the job was accepted; neither reports callback completion.

◆ ThreadJob_create()

BA_API ThreadJob * ThreadJob_create ( size_t  size,
ThreadJob_Run  run 
)

Create a generic thread job.

Parameters
sizeat least sizeof(ThreadJob)
runRequired callback. Your callback function will execute in the context of a thread within the Thread Manager
Returns
The allocated job, or NULL if memory allocation fails. Do not queue a NULL job. Additional payload storage is uninitialized. Before queuing, the caller owns the allocation and may release it with baFree. Queuing transfers ownership to the manager, which frees it after the callback.

◆ ThreadJob_lcreate()

BA_API ThreadJob * ThreadJob_lcreate ( size_t  size,
ThreadJob_LRun  lrun 
)

Create a thread job designed to execute Lua code.

Parameters
sizeat least sizeof(ThreadJob)
lrunRequired callback. Your callback function will execute in the context of a thread within the Thread Manager
Returns
The allocated job, or NULL if memory allocation fails. Do not queue a NULL job. Additional payload storage is uninitialized. Before queuing, the caller owns the allocation and may release it with baFree. Queuing transfers ownership to the manager, which frees it after the callback.