mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
reserach st: refine key.c
This commit is contained in:
parent
fd0c85b324
commit
c38a545780
1 changed files with 37 additions and 36 deletions
|
@ -56,51 +56,52 @@ static int key_max = 0;
|
||||||
*/
|
*/
|
||||||
int st_key_create(int *keyp, _st_destructor_t destructor)
|
int st_key_create(int *keyp, _st_destructor_t destructor)
|
||||||
{
|
{
|
||||||
if (key_max >= ST_KEYS_MAX) {
|
if (key_max >= ST_KEYS_MAX) {
|
||||||
errno = EAGAIN;
|
errno = EAGAIN;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
*keyp = key_max++;
|
*keyp = key_max++;
|
||||||
_st_destructors[*keyp] = destructor;
|
_st_destructors[*keyp] = destructor;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int st_key_getlimit(void)
|
int st_key_getlimit(void)
|
||||||
{
|
{
|
||||||
return ST_KEYS_MAX;
|
return ST_KEYS_MAX;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int st_thread_setspecific(int key, void *value)
|
int st_thread_setspecific(int key, void *value)
|
||||||
{
|
{
|
||||||
_st_thread_t *me = _ST_CURRENT_THREAD();
|
_st_thread_t *me = _ST_CURRENT_THREAD();
|
||||||
|
|
||||||
if (key < 0 || key >= key_max) {
|
if (key < 0 || key >= key_max) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
if (value != me->private_data[key]) {
|
|
||||||
/* free up previously set non-NULL data value */
|
|
||||||
if (me->private_data[key] && _st_destructors[key]) {
|
|
||||||
(*_st_destructors[key])(me->private_data[key]);
|
|
||||||
}
|
}
|
||||||
me->private_data[key] = value;
|
|
||||||
}
|
if (value != me->private_data[key]) {
|
||||||
|
/* free up previously set non-NULL data value */
|
||||||
return 0;
|
if (me->private_data[key] && _st_destructors[key]) {
|
||||||
|
(*_st_destructors[key])(me->private_data[key]);
|
||||||
|
}
|
||||||
|
me->private_data[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void *st_thread_getspecific(int key)
|
void *st_thread_getspecific(int key)
|
||||||
{
|
{
|
||||||
if (key < 0 || key >= key_max)
|
if (key < 0 || key >= key_max) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
return ((_ST_CURRENT_THREAD())->private_data[key]);
|
|
||||||
|
return ((_ST_CURRENT_THREAD())->private_data[key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -109,13 +110,13 @@ void *st_thread_getspecific(int key)
|
||||||
*/
|
*/
|
||||||
void _st_thread_cleanup(_st_thread_t *thread)
|
void _st_thread_cleanup(_st_thread_t *thread)
|
||||||
{
|
{
|
||||||
int key;
|
int key;
|
||||||
|
|
||||||
for (key = 0; key < key_max; key++) {
|
for (key = 0; key < key_max; key++) {
|
||||||
if (thread->private_data[key] && _st_destructors[key]) {
|
if (thread->private_data[key] && _st_destructors[key]) {
|
||||||
(*_st_destructors[key])(thread->private_data[key]);
|
(*_st_destructors[key])(thread->private_data[key]);
|
||||||
thread->private_data[key] = NULL;
|
thread->private_data[key] = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue