mUPnP for C
thread.h File Reference
#include <mupnp/typedef.h>
#include <mupnp/util/cond.h>
#include <mupnp/util/list.h>
#include <mupnp/util/time.h>
#include <pthread.h>
#include <signal.h>

Data Structures

struct  _mUpnpThread
 The generic wrapper struct for mUPnP's threads. More...
 

Macros

#define MUPNP_THREAD_MIN_SLEEP   1000
 
#define mupnp_thread_next(thread)
 
#define mupnp_thread_remove(thread)
 
#define mupnp_threadlist_clear(threadList)
 
#define mupnp_threadlist_size(threadList)
 
#define mupnp_threadlist_gets(threadList)
 
#define mupnp_threadlist_add(threadList, thread)
 
#define mupnp_threadlist_remove(thread)
 

Typedefs

typedef struct _mUpnpThread mUpnpThread
 The generic wrapper struct for mUPnP's threads.
 
typedef struct _mUpnpThread mUpnpThreadList
 
typedef void(* MUPNP_THREAD_FUNC) (mUpnpThread *)
 

Functions

mUpnpThreadmupnp_thread_new (void)
 Create a new thread instance.
 
mUpnpThreadmupnp_thread_self (void)
 Get the mUpnpThread object for the currently executing thread.
 
bool mupnp_thread_delete (mUpnpThread *thread)
 Stop and destroy a thread, freeing all resources.
 
bool mupnp_thread_start (mUpnpThread *thread)
 Start a thread's execution.
 
bool mupnp_thread_stop (mUpnpThread *thread)
 Request a thread to stop execution.
 
bool mupnp_thread_stop_with_cond (mUpnpThread *thread, mUpnpCond *cond)
 
bool mupnp_thread_restart (mUpnpThread *thread)
 
bool mupnp_thread_isrunnable (mUpnpThread *thread)
 
void mupnp_thread_setaction (mUpnpThread *thread, MUPNP_THREAD_FUNC actionFunc)
 
void mupnp_thread_setuserdata (mUpnpThread *thread, void *data)
 
void * mupnp_thread_getuserdata (mUpnpThread *thread)
 
mUpnpThreadListmupnp_threadlist_new (void)
 
void mupnp_threadlist_delete (mUpnpThreadList *threadList)
 
bool mupnp_threadlist_start (mUpnpThreadList *threadList)
 
bool mupnp_threadlist_stop (mUpnpThreadList *threadList)
 

Macro Definition Documentation

◆ MUPNP_THREAD_MIN_SLEEP

#define MUPNP_THREAD_MIN_SLEEP   1000

◆ mupnp_thread_next

#define mupnp_thread_next ( thread)
Value:
mUpnpList * mupnp_list_next(mUpnpList *list)
Definition list.c:234
The generic list interface in mUPnP.
Definition list.h:56
The generic wrapper struct for mUPnP's threads.
Definition thread.h:64

◆ mupnp_thread_remove

#define mupnp_thread_remove ( thread)
Value:
void mupnp_list_remove(mUpnpList *list)
Remove a node from its list.
Definition list.c:144

◆ mupnp_threadlist_add

#define mupnp_threadlist_add ( threadList,
thread )
Value:
mupnp_list_add((mUpnpList*)threadList, (mUpnpList*)thread)
void mupnp_list_add(mUpnpList *headList, mUpnpList *list)
Add a node to the end of a list.
Definition list.c:123

Add a thread into a thread list

Parameters
threadListThe thread list in question
threadThe thread to add to the list

◆ mupnp_threadlist_clear

#define mupnp_threadlist_clear ( threadList)
Value:
void mupnp_list_clear(mUpnpList *headList, MUPNP_LIST_DESTRUCTORFUNC destructorFunc)
Definition list.c:257
void(* MUPNP_LIST_DESTRUCTORFUNC)(void *)
Definition list.h:29
bool mupnp_thread_delete(mUpnpThread *thread)
Stop and destroy a thread, freeing all resources.
Definition thread.c:218

Clear the contents of a thread list

Parameters
threadListThread list in question

◆ mupnp_threadlist_gets

#define mupnp_threadlist_gets ( threadList)
Value:

Get the first actual item from a thread list to use as an iterator

Parameters
threadListThe thread list in question

◆ mupnp_threadlist_remove

#define mupnp_threadlist_remove ( thread)
Value:

Remove a thread from thread list

Parameters
threadListThe thread list in question
threadThe thread to be removed

◆ mupnp_threadlist_size

#define mupnp_threadlist_size ( threadList)
Value:
int mupnp_list_size(mUpnpList *headList)
Definition list.c:53

Get the size of a thread list

Parameters
threadListThe thread list in question

Typedef Documentation

◆ MUPNP_THREAD_FUNC

typedef void(* MUPNP_THREAD_FUNC) (mUpnpThread *)

Prototype for the threads' worker functions

◆ mUpnpThread

typedef struct _mUpnpThread mUpnpThread

The generic wrapper struct for mUPnP's threads.

This wrapper has been created to enable 100% code compatibility between different platforms (Linux, Win32 etc..)

◆ mUpnpThreadList

typedef struct _mUpnpThread mUpnpThreadList

Function Documentation

◆ mupnp_thread_delete()

bool mupnp_thread_delete ( mUpnpThread * thread)

Stop and destroy a thread, freeing all resources.

Stops the thread if it is running and releases all associated resources. This function blocks until the thread has fully terminated.

The thread's action function should periodically check mupnp_thread_isrunnable() and exit when it returns false to ensure graceful shutdown.

Parameters
threadThe thread to destroy. May be NULL (no-op if NULL).
Return values
trueSuccessfully stopped and destroyed the thread
falseFailed to destroy (e.g., thread is NULL)
Note
After calling this function, the thread pointer is invalid.
Blocking behavior: This function blocks until the thread terminates. If the thread does not check mupnp_thread_isrunnable(), it may block indefinitely.
Thread-safe: Must not be called concurrently on the same thread.
Warning
Do not call this function from the thread's own action function; this will cause deadlock.
See also
mupnp_thread_new()
mupnp_thread_stop()

◆ mupnp_thread_getuserdata()

void * mupnp_thread_getuserdata ( mUpnpThread * thread)

Get the user data pointer

Parameters
threadThread from which to get the pointer

◆ mupnp_thread_isrunnable()

bool mupnp_thread_isrunnable ( mUpnpThread * thread)

Check if a thread has been started

Parameters
threadThread to check

◆ mupnp_thread_new()

mUpnpThread * mupnp_thread_new ( void )

Create a new thread instance.

Allocates and initializes a platform-independent thread object. The thread is initially in a stopped state (not running).

Before starting the thread, you must:

  1. Set an action function with mupnp_thread_setaction()
  2. Optionally set user data with mupnp_thread_setuserdata()

Platform support:

  • Linux/Unix: pthread (POSIX threads)
  • Windows: CreateThread API
  • ITRON, T-Engine, BTRON: platform-specific task APIs
Returns
A newly-created mUpnpThread on success, or NULL if thread creation fails (e.g., insufficient system resources).
Note
The returned thread must be freed with mupnp_thread_delete() when no longer needed.
Thread-safe: Can be called concurrently from multiple threads.
The thread does not start automatically; call mupnp_thread_start() to begin execution.
See also
mupnp_thread_delete()
mupnp_thread_start()
mupnp_thread_setaction()
// Example: Create and start a worker thread
void worker_function(mUpnpThread* thread) {
while (mupnp_thread_isrunnable(thread)) {
// Perform work...
}
}
mupnp_thread_setaction(thread, worker_function);
void mupnp_thread_setaction(mUpnpThread *thread, MUPNP_THREAD_FUNC func)
Definition thread.c:535
mUpnpThread * mupnp_thread_new(void)
Create a new thread instance.
Definition thread.c:181
bool mupnp_thread_start(mUpnpThread *thread)
Start a thread's execution.
Definition thread.c:273
bool mupnp_thread_isrunnable(mUpnpThread *thread)
Definition thread.c:518
#define mupnp_sleep(val)
Definition time.h:71

◆ mupnp_thread_restart()

bool mupnp_thread_restart ( mUpnpThread * thread)

Restart a thread. Essentially calls mupnp_thread_stop() and mupnp_thread_start()

Parameters
threadThread to restart

◆ mupnp_thread_self()

mUpnpThread * mupnp_thread_self ( void )

Get the mUpnpThread object for the currently executing thread.

Returns a reference to the thread structure for the calling thread. This is useful when a thread needs to access its own thread data or check if it should continue running.

Returns
Pointer to the current thread's mUpnpThread structure, or NULL if the calling thread was not created with mupnp_thread_new().
Note
Thread-safe: Can be called from any thread.
This function only works for threads created by the mUPnP library; external threads (e.g., main thread, OS threads) will return NULL.
See also
mupnp_thread_isrunnable()

◆ mupnp_thread_setaction()

void mupnp_thread_setaction ( mUpnpThread * thread,
MUPNP_THREAD_FUNC actionFunc )

Set the thread's worker function.

Parameters
threadThread struct
actionFuncFunction pointer to set as the worker function

◆ mupnp_thread_setuserdata()

void mupnp_thread_setuserdata ( mUpnpThread * thread,
void * data )

Set the user data pointer

Parameters
threadThread struct
dataPointer to user data

◆ mupnp_thread_start()

bool mupnp_thread_start ( mUpnpThread * thread)

Start a thread's execution.

Starts the thread, causing its action function to begin execution in a new thread of control. The thread must have been created with mupnp_thread_new() and must have an action function set with mupnp_thread_setaction().

After starting, the action function runs concurrently with the calling thread until the thread is stopped or the action function returns.

Parameters
threadThe thread to start. Must not be NULL and must have an action function set.
Return values
trueSuccessfully started the thread
falseFailed to start due to:
  • thread is NULL
  • No action function set
  • Thread already running
  • System error (e.g., cannot create OS thread)
Note
Thread-safe: Can be called from any thread.
Calling this function on an already-running thread has no effect and returns false.
Side effect: Sets the thread's runnable flag to true and creates a new OS-level thread.
See also
mupnp_thread_stop()
mupnp_thread_isrunnable()
mupnp_thread_setaction()

◆ mupnp_thread_stop()

bool mupnp_thread_stop ( mUpnpThread * thread)

Request a thread to stop execution.

Signals the thread to stop by setting its runnable flag to false. This function does NOT forcibly terminate the thread; the thread's action function must cooperatively check mupnp_thread_isrunnable() and exit when it returns false.

This function returns immediately without waiting for the thread to terminate. Use mupnp_thread_delete() if you need to wait for termination.

Parameters
threadThe thread to stop. Must not be NULL.
Return values
trueSuccessfully signaled the thread to stop
falseFailed to signal (e.g., thread is NULL)
Note
Non-blocking: Returns immediately without waiting for thread exit.
Thread-safe: Can be called from any thread, including the thread being stopped.
Side effect: Sets the thread's runnable flag to false.
The thread continues running until its action function checks mupnp_thread_isrunnable() and exits.
See also
mupnp_thread_start()
mupnp_thread_isrunnable()
mupnp_thread_delete()

◆ mupnp_thread_stop_with_cond()

bool mupnp_thread_stop_with_cond ( mUpnpThread * thread,
mUpnpCond * cond )

Stop the running thread and signal the given CGCond.

◆ mupnp_threadlist_delete()

void mupnp_threadlist_delete ( mUpnpThreadList * threadList)

Destroy a thread list

Parameters
threadListThe thread list in question

◆ mupnp_threadlist_new()

mUpnpThreadList * mupnp_threadlist_new ( void )

Create a new thread list

Returns
Thread list

◆ mupnp_threadlist_start()

bool mupnp_threadlist_start ( mUpnpThreadList * threadList)

Start all threads in the thread list

Parameters
threadListThe thread list in question

◆ mupnp_threadlist_stop()

bool mupnp_threadlist_stop ( mUpnpThreadList * threadList)

Stop all threads in the thread list

Parameters
threadListThe thread list in question