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
635f7026
Commit
635f7026
authored
Aug 25, 2011
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
io_thread: add helper functions
parent
0f1e4f03
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
0 deletions
+84
-0
io_thread.c
src/io_thread.c
+71
-0
io_thread.h
src/io_thread.h
+13
-0
No files found.
src/io_thread.c
View file @
635f7026
...
...
@@ -18,10 +18,14 @@
*/
#include "io_thread.h"
#include "glib_compat.h"
#include <assert.h>
static
struct
{
GMutex
*
mutex
;
GCond
*
cond
;
GMainContext
*
context
;
GMainLoop
*
loop
;
GThread
*
thread
;
...
...
@@ -44,6 +48,8 @@ io_thread_init(void)
assert
(
io
.
loop
==
NULL
);
assert
(
io
.
thread
==
NULL
);
io
.
mutex
=
g_mutex_new
();
io
.
cond
=
g_cond_new
();
io
.
context
=
g_main_context_new
();
io
.
loop
=
g_main_loop_new
(
io
.
context
,
false
);
}
...
...
@@ -77,6 +83,9 @@ io_thread_deinit(void)
if
(
io
.
context
!=
NULL
)
g_main_context_unref
(
io
.
context
);
g_cond_free
(
io
.
cond
);
g_mutex_free
(
io
.
mutex
);
}
GMainContext
*
...
...
@@ -85,3 +94,65 @@ io_thread_context(void)
return
io
.
context
;
}
guint
io_thread_idle_add
(
GSourceFunc
function
,
gpointer
data
)
{
GSource
*
source
=
g_idle_source_new
();
g_source_set_callback
(
source
,
function
,
data
,
NULL
);
guint
id
=
g_source_attach
(
source
,
io
.
context
);
g_source_unref
(
source
);
return
id
;
}
guint
io_thread_timeout_add_seconds
(
guint
interval
,
GSourceFunc
function
,
gpointer
data
)
{
GSource
*
source
=
g_timeout_source_new_seconds
(
interval
);
g_source_set_callback
(
source
,
function
,
data
,
NULL
);
guint
id
=
g_source_attach
(
source
,
io
.
context
);
g_source_unref
(
source
);
return
id
;
}
struct
call_data
{
GThreadFunc
function
;
gpointer
data
;
bool
done
;
gpointer
result
;
};
static
gboolean
io_thread_call_func
(
gpointer
_data
)
{
struct
call_data
*
data
=
_data
;
gpointer
result
=
data
->
function
(
data
->
data
);
g_mutex_lock
(
io
.
mutex
);
data
->
done
=
true
;
data
->
result
=
result
;
g_cond_broadcast
(
io
.
cond
);
g_mutex_unlock
(
io
.
mutex
);
return
false
;
}
gpointer
io_thread_call
(
GThreadFunc
function
,
gpointer
_data
)
{
struct
call_data
data
=
{
.
function
=
function
,
.
data
=
_data
,
.
done
=
false
,
};
io_thread_idle_add
(
io_thread_call_func
,
&
data
);
g_mutex_lock
(
io
.
mutex
);
while
(
!
data
.
done
)
g_cond_wait
(
io
.
cond
,
io
.
mutex
);
g_mutex_unlock
(
io
.
mutex
);
return
data
.
result
;
}
src/io_thread.h
View file @
635f7026
...
...
@@ -36,4 +36,17 @@ G_GNUC_PURE
GMainContext
*
io_thread_context
(
void
);
guint
io_thread_idle_add
(
GSourceFunc
function
,
gpointer
data
);
guint
io_thread_timeout_add_seconds
(
guint
interval
,
GSourceFunc
function
,
gpointer
data
);
/**
* Call a function synchronously in the I/O thread.
*/
gpointer
io_thread_call
(
GThreadFunc
function
,
gpointer
data
);
#endif
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