sphthread.h File Reference

Thread utility functions. More...

`#include <unistd.h>`

Go to the source code of this file.

## Functions

__C__ pid_t | sphdeGetPID (void)
| Return the process identification. More...
__C__ pid_t | sphdeGetTID (void)
| Return the thread identification. More...
__C__ char * | sphdeGetCmdLine (void)
| Return the command line string. More...
static int | sphFastGetPID (void)
| Return the process identification. More...
__thread pid_t threadID | __attribute__ ((tls_model("initial-exec")))
static int | sphFastGetTID (void)
| Return the thread identification. More...

## Variables

pid_t | procID

## Detailed Description

Thread utility functions.

The process pid can be obtained using sphdeGetPID, thread id by sphdeGetTID, and command line string by sphdeGetCmdLine. Fast versions that access static variables and avoid syscall where possible are provided: sphFastGetPID for process id and sphFastGetTID for thread id.

For instance, to provide a fast event log header:

struct eventHeader
{
unsigned int eventID;
unsigned short pid;
unsigned short tid;
unsigned sphtimer_t timestamp;
} evh;
evh.eventID = 0;
evh.pid = sphFastGetPID ();
evh.tid = sphFastGetTID ();
evh.timestamp = sphgettimer ();
sphFastGetTID
static int sphFastGetTID(void)
Return the thread identification.
Definition: sphthread.h:128
sphFastGetPID
static int sphFastGetPID(void)
Return the process identification.
Definition: sphthread.h:104
sphtimer_t
unsigned long long int sphtimer_t
Value from TB/TSC register (64-bits on all platforms).
Definition: sphtimer.h:66
sphgettimer
static sphtimer_t sphgettimer(void)
Read and return the Timebase value.
Definition: sphtimer.h:139

## Function Documentation

## __attribute__()

__thread pid_t threadID __attribute__ | ( | (tls_model("initial-exec")) |  | ) |

Per-thread thread identification variable. Should not be accessed directly, instead the function sphFastGetTID should be used.

## sphdeGetCmdLine()

__C__ char * sphdeGetCmdLine | ( | void |  | ) |

Return the command line string.

The command line is obtained from '/proc/pid/cmdline' and maintained in an internal static variable.

Returns
  A null-terminated string with the process command line.

## sphdeGetPID()

__C__ pid_t sphdeGetPID | ( | void |  | ) |

Return the process identification.

The function issues a syscall when needed and it also update its internal command line value (so a subsequent call to sphdeGetCmdLine will just return the already available command line value).

Returns
  The process id or 0 if an error occurs.

## sphdeGetTID()

__C__ pid_t sphdeGetTID | ( | void |  | ) |

Return the thread identification.

The function issues a syscall when needed. The thread identification value is a per-thread variable so a subsequent call to sphFastGetTID will just need to return a variable value (no syscall needed).

Returns
  The thread identification or -1 if an error occurs.

## sphFastGetPID()

static int sphFastGetPID | ( | void |  | ) |

inline static

Return the process identification.

Fast version of sphdeGetPID.

Returns
  The process id or 0 if an error occurs.

## sphFastGetTID()

static int sphFastGetTID | ( | void |  | ) |

inline static

Return the thread identification.

Fast version of sphdeGetTID.

Returns
  The thread identification or -1 if an error occurs.

## Variable Documentation

## procID

pid_t procID

extern

Process identification variable. Should not be accessed directly, instead the function sphFastGetPID should be used.