Skip to content

Commit ba5361a

Browse files
committed
Use pthread instead of C++11 threads for better compatibility.
Newer libuv has cross-platform threads with similar API.
1 parent 2ed4a82 commit ba5361a

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

src/stream.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "device.h"
77
#include "uv_async_queue.h"
88
#include <vector>
9-
#include <atomic>
109

1110
namespace NodeUsb {
1211

@@ -30,7 +29,7 @@ namespace NodeUsb {
3029
void stop();
3130

3231
enum StreamStatus {STREAM_IDLE, STREAM_ACTIVE, STREAM_CANCELLING, STREAM_ABORTED};
33-
std::atomic_uchar state;
32+
volatile uint8_t state;
3433
unsigned activeTransfers;
3534

3635
protected:

src/usb.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#include "usb.h"
22
#include "device.h"
3-
#include <thread>
3+
#include <uv.h>
4+
#include <pthread.h>
45

56
namespace NodeUsb {
67
libusb_context* usb_context;
7-
std::thread usb_thread;
8+
pthread_t usb_thread_id;
89
std::vector< Persistent<Object> > Usb::deviceList;
910

10-
void USBThreadFn(){
11+
void* USBThreadFn(void* v){
1112
while(1) libusb_handle_events(usb_context);
13+
return NULL;
1214
}
1315

1416
void Usb::Initalize(Handle<Object> target) {
@@ -75,8 +77,8 @@ namespace NodeUsb {
7577
// Bindings to nodejs
7678
NODE_SET_METHOD(target, "setDebugLevel", Usb::SetDebugLevel);
7779

78-
usb_thread = std::thread(USBThreadFn);
79-
usb_thread.detach();
80+
pthread_create(&usb_thread_id, NULL, USBThreadFn, NULL);
81+
pthread_detach(usb_thread_id);
8082

8183
Local<ObjectTemplate> devlist_tpl = ObjectTemplate::New();
8284
devlist_tpl->SetAccessor(V8STR("length"), DeviceListLength);

src/uv_async_queue.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,50 @@
44
#include <uv.h>
55
#include <functional>
66
#include <queue>
7-
#include <thread>
8-
#include <mutex>
7+
#include <pthread.h>
98

109
template <class T>
1110
class UVQueue{
1211
public:
1312
UVQueue(std::function<void(T&)> cb, bool _keep_alive=false): callback(cb), keep_alive(_keep_alive) {
1413
uv_async_init(uv_default_loop(), &async, UVQueue::internal_callback);
14+
pthread_mutex_init(&mutex, NULL);
1515
async.data = this;
1616
if (!keep_alive) uv_unref(uv_default_loop());
1717
}
1818

1919
void post(T value){
20-
mutex.lock();
20+
pthread_mutex_lock(&mutex);
2121
queue.push(value);
22-
mutex.unlock();
22+
pthread_mutex_unlock(&mutex);
2323
uv_async_send(&async);
2424
}
2525

2626
~UVQueue(){
2727
if (!keep_alive) uv_ref(uv_default_loop());
2828
uv_close((uv_handle_t*)&async, NULL); //TODO: maybe we can't delete UVQueue until callback?
29+
pthread_mutex_destroy(&mutex);
2930
}
3031

3132
private:
3233
std::function<void(T&)> callback;
3334
std::queue<T> queue;
34-
std::mutex mutex;
35+
pthread_mutex_t mutex;
3536
uv_async_t async;
3637
bool keep_alive;
3738

3839
static void internal_callback(uv_async_t *handle, int status){
3940
UVQueue* uvqueue = static_cast<UVQueue*>(handle->data);
4041

4142
while(1){
42-
uvqueue->mutex.lock();
43+
pthread_mutex_lock(&uvqueue->mutex);
4344
if (uvqueue->queue.empty()){
44-
uvqueue->mutex.unlock();
45+
pthread_mutex_unlock(&uvqueue->mutex);
4546
break;
4647
}
4748
T item = uvqueue->queue.front();
4849
uvqueue->queue.pop();
49-
uvqueue->mutex.unlock();
50+
pthread_mutex_unlock(&uvqueue->mutex);
5051
uvqueue->callback(item);
5152
}
5253
}

wscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def configure(conf):
2121

2222
conf.check_cfg(package='libusb-1.0', uselib_store='USB10', mandatory=1, args='--cflags --libs')
2323
conf.env.append_unique('CPPFLAGS', ["-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE"])
24-
conf.env.append_unique('CXXFLAGS', ["-Wall", "-std=gnu++0x"])
24+
conf.env.append_unique('CXXFLAGS', ["-Wall", "-std=c++0x"])
2525
conf.env.append_value('CPPFLAGS_NODE', ['-DEV_MULTIPLICITY=1'])
2626

2727
def build(bld):

0 commit comments

Comments
 (0)