Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
vfs_async.c
Go to the documentation of this file.
1 /*
2  * vfs_async.c
3  * Copyright 2010 William Pitcock
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions, and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions, and the following disclaimer in the documentation
13  * provided with the distribution.
14  *
15  * This software is provided "as is" and without any warranty, express or
16  * implied. In no event shall the authors be liable for any damages arising from
17  * the use of this software.
18  */
19 
20 #include <glib.h>
21 #include <pthread.h>
22 
23 #include "vfs_async.h"
24 
25 typedef struct {
26  char * filename; /* pooled */
27  void *buf;
28  int64_t size;
29  pthread_t thread;
30  void * userdata;
31 
34 
35 bool_t
37 {
38  VFSAsyncTrampoline *tr = data;
39 
40  pthread_join (tr->thread, NULL);
41 
42  tr->cons_f(tr->buf, tr->size, tr->userdata);
43 
44  str_unref (tr->filename);
45  g_slice_free(VFSAsyncTrampoline, tr);
46 
47  return FALSE;
48 }
49 
50 void *
52 {
53  VFSAsyncTrampoline *tr = data;
54 
55  vfs_file_get_contents(tr->filename, &tr->buf, &tr->size);
56 
57  g_idle_add_full(G_PRIORITY_HIGH_IDLE, vfs_async_file_get_contents_trampoline, tr, NULL);
58 
59  return NULL;
60 }
61 
62 EXPORT void
63 vfs_async_file_get_contents(const char *filename, VFSConsumer cons_f, void * userdata)
64 {
66 
67  tr = g_slice_new0(VFSAsyncTrampoline);
68  tr->filename = str_get (filename);
69  tr->cons_f = cons_f;
70  tr->userdata = userdata;
71 
72  pthread_create (& tr->thread, NULL, vfs_async_file_get_contents_worker, tr);
73 }
const char filename
Definition: misc-api.h:85
VFSConsumer cons_f
Definition: vfs_async.c:32
#define FALSE
Definition: core.h:37
pthread_t thread
Definition: vfs_async.c:29
Index Index bool_t
Definition: playlist-api.h:122
void * vfs_async_file_get_contents_worker(void *data)
Definition: vfs_async.c:51
#define NULL
Definition: core.h:29
EXPORT void vfs_async_file_get_contents(const char *filename, VFSConsumer cons_f, void *userdata)
Definition: vfs_async.c:63
void str_unref(char *str)
Definition: strpool.c:131
bool_t(* VFSConsumer)(void *buf, int64_t size, void *userdata)
Definition: vfs_async.h:25
void vfs_file_get_contents(const char *filename, void **buf, int64_t *size)
Gets contents of the file into a buffer.
Definition: vfs_common.c:149
bool_t vfs_async_file_get_contents_trampoline(void *data)
Definition: vfs_async.c:36
char * str_get(const char *str)
Definition: strpool.c:68