Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
ce5c22f4
Commit
ce5c22f4
authored
Dec 28, 2008
by
Thomas Jansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
condition: migrate from pthread to glib threads
parent
248cd50a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
36 deletions
+27
-36
condition.c
src/condition.c
+24
-33
condition.h
src/condition.h
+3
-3
No files found.
src/condition.c
View file @
ce5c22f4
...
...
@@ -26,53 +26,47 @@
void
cond_init
(
struct
condition
*
cond
)
{
int
err
;
if
((
err
=
pthread_mutex_init
(
&
cond
->
mutex
,
NULL
)))
FATAL
(
"failed to init mutex: %s
\n
"
,
strerror
(
err
));
if
((
err
=
pthread_cond_init
(
&
cond
->
cond
,
NULL
)))
FATAL
(
"failed to init cond: %s
\n
"
,
strerror
(
err
));
cond
->
mutex
=
g_mutex_new
();
if
(
cond
->
mutex
==
NULL
)
FATAL
(
"g_mutex_new failed"
);
cond
->
cond
=
g_cond_new
();
if
(
cond
->
cond
==
NULL
)
FATAL
(
"g_cond_new() failed"
);
}
void
cond_enter
(
struct
condition
*
cond
)
{
pthread_mutex_lock
(
&
cond
->
mutex
);
g_mutex_lock
(
cond
->
mutex
);
}
void
cond_leave
(
struct
condition
*
cond
)
{
pthread_mutex_unlock
(
&
cond
->
mutex
);
g_mutex_unlock
(
cond
->
mutex
);
}
void
cond_wait
(
struct
condition
*
cond
)
{
pthread_cond_wait
(
&
cond
->
cond
,
&
cond
->
mutex
);
}
static
struct
timespec
*
ts_timeout
(
struct
timespec
*
ts
,
const
long
sec
)
{
struct
timeval
tv
;
gettimeofday
(
&
tv
,
NULL
);
ts
->
tv_sec
=
tv
.
tv_sec
+
sec
;
ts
->
tv_nsec
=
tv
.
tv_usec
*
1000
;
return
ts
;
g_cond_wait
(
cond
->
cond
,
cond
->
mutex
);
}
int
cond_timedwait
(
struct
condition
*
cond
,
const
long
sec
)
{
struct
timespec
ts
;
int
ret
=
pthread_cond_timedwait
(
&
cond
->
cond
,
&
cond
->
mutex
,
ts_timeout
(
&
ts
,
sec
));
if
(
!
ret
||
ret
==
ETIMEDOUT
)
return
ret
;
FATAL
(
"cond_timedwait: %s
\n
"
,
strerror
(
ret
));
return
ret
;
GTimeVal
t
;
g_get_current_time
(
&
t
);
g_time_val_add
(
&
t
,
sec
*
1000000
);
if
(
g_cond_timed_wait
(
cond
->
cond
,
cond
->
mutex
,
&
t
)
==
FALSE
)
return
ETIMEDOUT
;
return
0
;
}
int
cond_signal_async
(
struct
condition
*
cond
)
{
if
(
!
pthread_mutex_trylock
(
&
cond
->
mutex
)
)
{
pthread_cond_signal
(
&
cond
->
cond
);
pthread_mutex_unlock
(
&
cond
->
mutex
);
if
(
g_mutex_trylock
(
cond
->
mutex
)
==
FALSE
)
{
g_cond_signal
(
cond
->
cond
);
g_mutex_unlock
(
cond
->
mutex
);
return
0
;
}
return
EBUSY
;
...
...
@@ -80,14 +74,11 @@ int cond_signal_async(struct condition *cond)
void
cond_signal_sync
(
struct
condition
*
cond
)
{
pthread_cond_signal
(
&
cond
->
cond
);
g_cond_signal
(
cond
->
cond
);
}
void
cond_destroy
(
struct
condition
*
cond
)
{
int
err
;
if
((
err
=
pthread_cond_destroy
(
&
cond
->
cond
)))
FATAL
(
"failed to destroy cond: %s
\n
"
,
strerror
(
err
));
if
((
err
=
pthread_mutex_destroy
(
&
cond
->
mutex
)))
FATAL
(
"failed to destroy mutex: %s
\n
"
,
strerror
(
err
));
g_mutex_free
(
cond
->
mutex
);
g_cond_free
(
cond
->
cond
);
}
src/condition.h
View file @
ce5c22f4
...
...
@@ -20,11 +20,11 @@
#ifndef MPD_CONDITION_H
#define MPD_CONDITION_H
#include <
pthread
.h>
#include <
glib
.h>
struct
condition
{
pthread_mutex_t
mutex
;
pthread_cond_t
cond
;
GMutex
*
mutex
;
GCond
*
cond
;
};
void
cond_init
(
struct
condition
*
cond
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment