Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion depends/README.depends.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ libusbx 1.0.17
with superspeed patch by Joshua Blake <joshblake@gmail.com> see https://github.com/JoshBlake/libusbx/tree/superspeed
with MAX_ISO_BUFFER_LENGTH increased to 49152 in libusb/os/libusb_usbfs.h
turbojpeg
boost threads
opencv

run ./depends/install_ubuntu.sh
Expand Down
12 changes: 7 additions & 5 deletions examples/protonect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ SET(MY_DIR ${libfreenect2_SOURCE_DIR})
# additional cmake modules
LIST(APPEND CMAKE_MODULE_PATH ${MY_DIR}/cmake_modules)

# setup threading
INCLUDE(SetupLibfreenect2Threading)
INCLUDE_DIRECTORIES(${LIBFREENECT2_THREADING_INCLUDE_DIR})

#set the default path for built executables to the "bin" directory
SET(EXECUTABLE_OUTPUT_PATH ${MY_DIR}/bin)

# dependencies
FIND_PACKAGE(OpenCV REQUIRED)
FIND_PACKAGE(Boost REQUIRED)

# OpenCV
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIR})

# Boost
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )

# LibUSB
INCLUDE_DIRECTORIES("${MY_DIR}/../../depends/libusbx/include/libusb-1.0/")
LINK_DIRECTORIES("${MY_DIR}/../../depends/libusbx/lib/")
Expand All @@ -43,11 +43,13 @@ ADD_EXECUTABLE(Protonect
src/cpu_depth_packet_processor.cpp

Protonect.cpp

${LIBFREENECT2_THREADING_SOURCE}
)

TARGET_LINK_LIBRARIES(Protonect
usb-1.0
boost_thread
${OpenCV_LIBS}
turbojpeg
${LIBFREENECT2_THREADING_LIBRARIES}
)
6 changes: 1 addition & 5 deletions examples/protonect/Protonect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

#include "Protonect.h"

#include <boost/thread.hpp>

#include <sstream>
#include <iostream>
#include <fstream>
Expand All @@ -40,8 +38,6 @@

#include <opencv2/opencv.hpp>

#include <boost/bind.hpp>

#include <libfreenect2/tables.h>
#include <libfreenect2/usb/event_loop.h>
#include <libfreenect2/usb/transfer_pool.h>
Expand Down Expand Up @@ -798,7 +794,7 @@ int main(int argc, char *argv[])

// wait for all transfers to cancel
// TODO: better implementation
boost::this_thread::sleep(boost::posix_time::seconds(2));
libfreenect2::this_thread::sleep_for(libfreenect2::chrono::seconds(2));

rgb_bulk_transfers.deallocate();
depth_iso_transfers.deallocate();
Expand Down
35 changes: 35 additions & 0 deletions examples/protonect/cmake_modules/SetupLibfreenect2Threading.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
INCLUDE(CheckCXXSourceCompiles)

CHECK_CXX_SOURCE_COMPILES("
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>

int main(int argc, char** argv) {
std::thread thread;
std::mutex mutex;
std::lock_guard<std::mutex> lock_guard(mutex);
std::unique_lock<std::mutex> unique_lock(mutex);
std::condition_variable condition_variable;

return 0;
}

" LIBFREENECT2_THREADING_STDLIB)

IF(LIBFREENECT2_THREADING_STDLIB)
SET(LIBFREENECT2_THREADING "stdlib")
SET(LIBFREENECT2_THREADING_INCLUDE_DIR "")
SET(LIBFREENECT2_THREADING_SOURCE "")
SET(LIBFREENECT2_THREADING_LIBRARIES "")
ADD_DEFINITIONS(-DLIBFREENECT2_THREADING_STDLIB)
ELSE(LIBFREENECT2_THREADING_STDLIB)
SET(LIBFREENECT2_THREADING "tinythread")
SET(LIBFREENECT2_THREADING_INCLUDE_DIR "src/tinythread/")
SET(LIBFREENECT2_THREADING_SOURCE "src/tinythread/tinythread.cpp")
SET(LIBFREENECT2_THREADING_LIBRARIES "")
ADD_DEFINITIONS(-DLIBFREENECT2_THREADING_TINYTHREAD)
ENDIF(LIBFREENECT2_THREADING_STDLIB)

MESSAGE(STATUS "using ${LIBFREENECT2_THREADING} as threading library")
21 changes: 13 additions & 8 deletions examples/protonect/include/libfreenect2/async_packet_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#ifndef ASYNC_PACKET_PROCESSOR_H_
#define ASYNC_PACKET_PROCESSOR_H_

#include <boost/thread.hpp>
#include <libfreenect2/threading.h>

namespace libfreenect2
{
Expand All @@ -42,7 +42,7 @@ class AsyncPacketProcessor
processor_(processor),
current_packet_available_(false),
shutdown_(false),
thread_(boost::bind(&AsyncPacketProcessor<PacketT, PacketProcessorT>::execute, this))
thread_(&AsyncPacketProcessor<PacketT, PacketProcessorT>::static_execute, this)
{
}
virtual ~AsyncPacketProcessor()
Expand All @@ -69,7 +69,7 @@ class AsyncPacketProcessor
void process(const PacketT &packet)
{
{
boost::mutex::scoped_lock l(packet_mutex_);
libfreenect2::lock_guard l(packet_mutex_);
current_packet_ = packet;
current_packet_available_ = true;
}
Expand All @@ -81,17 +81,22 @@ class AsyncPacketProcessor
PacketT current_packet_;

bool shutdown_;
boost::mutex packet_mutex_;
boost::condition_variable packet_condition_;
boost::thread thread_;
libfreenect2::mutex packet_mutex_;
libfreenect2::condition_variable packet_condition_;
libfreenect2::thread thread_;

static void static_execute(void *data)
{
static_cast<AsyncPacketProcessor<PacketT, PacketProcessorT> *>(data)->execute();
}

void execute()
{
boost::mutex::scoped_lock l(packet_mutex_);
libfreenect2::unique_lock l(packet_mutex_);

while(!shutdown_)
{
packet_condition_.wait(l);
WAIT_CONDITION(packet_condition_, packet_mutex_, l);

if(current_packet_available_)
{
Expand Down
6 changes: 3 additions & 3 deletions examples/protonect/include/libfreenect2/frame_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define FRAME_LISTENER_H_

#include <map>
#include <boost/thread.hpp>
#include <libfreenect2/threading.h>

namespace libfreenect2
{
Expand Down Expand Up @@ -75,8 +75,8 @@ class FrameListener

bool addNewFrame(Frame::Type type, Frame *frame);
private:
boost::mutex mutex_;
boost::condition_variable condition_;
libfreenect2::mutex mutex_;
libfreenect2::condition_variable condition_;
FrameMap next_frame_;

const unsigned int subscribed_frame_types_;
Expand Down
94 changes: 94 additions & 0 deletions examples/protonect/include/libfreenect2/threading.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* This file is part of the OpenKinect Project. http://www.openkinect.org
*
* Copyright (c) 2014 individual OpenKinect contributors. See the CONTRIB file
* for details.
*
* This code is licensed to you under the terms of the Apache License, version
* 2.0, or, at your option, the terms of the GNU General Public License,
* version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
* or the following URLs:
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.gnu.org/licenses/gpl-2.0.txt
*
* If you redistribute this file in source form, modified or unmodified, you
* may:
* 1) Leave this header intact and distribute it under the same terms,
* accompanying it with the APACHE20 and GPL20 files, or
* 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
* 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
* In all cases you must keep the copyright notice intact and include a copy
* of the CONTRIB file.
*
* Binary distributions must follow the binary distribution requirements of
* either License.
*/

#ifndef THREADING_H_
#define THREADING_H_

#ifdef LIBFREENECT2_THREADING_STDLIB

#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>

#define WAIT_CONDITION(var, mutex, lock) var.wait(lock);

namespace libfreenect2
{

typedef std::thread thread;
typedef std::mutex mutex;
typedef std::lock_guard<std::mutex> lock_guard;
typedef std::unique_lock<std::mutex> unique_lock;
typedef std::condition_variable condition_variable;

namespace chrono
{
using namespace std::chrono;
}

namespace this_thread
{
using namespace std::this_thread;
}

} /* libfreenect2 */

#endif

#ifdef LIBFREENECT2_THREADING_TINYTHREAD

#include <tinythread.h>

// TODO: work around for tinythread incompatibility
#define WAIT_CONDITION(var, mutex, lock) var.wait(mutex);

namespace libfreenect2
{

typedef tthread::thread thread;
typedef tthread::mutex mutex;
typedef tthread::lock_guard<tthread::mutex> lock_guard;
// TODO: this is not optimal
typedef tthread::lock_guard<tthread::mutex> unique_lock;
typedef tthread::condition_variable condition_variable;

namespace chrono
{
using namespace tthread::chrono;
}

namespace this_thread
{
using namespace tthread::this_thread;
}

} /* libfreenect2 */

#endif


#endif /* THREADING_H_ */
5 changes: 3 additions & 2 deletions examples/protonect/include/libfreenect2/usb/event_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#ifndef EVENT_LOOP_H_
#define EVENT_LOOP_H_

#include <boost/thread.hpp>
#include <libfreenect2/threading.h>

namespace libfreenect2
{
Expand All @@ -45,8 +45,9 @@ class EventLoop
void stop();
private:
bool shutdown_;
boost::thread *thread_;
libfreenect2::thread *thread_;

static void static_execute(void *cookie);
void execute();
};

Expand Down
8 changes: 6 additions & 2 deletions examples/protonect/src/event_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@

#include <libfreenect2/usb/event_loop.h>

#include <boost/bind.hpp>
#include <libusb.h>

namespace libfreenect2
{
namespace usb
{

void EventLoop::static_execute(void *cookie)
{
static_cast<EventLoop *>(cookie)->execute();
}

EventLoop::EventLoop() :
shutdown_(false),
thread_(0)
Expand All @@ -50,7 +54,7 @@ void EventLoop::start()
if(thread_ == 0)
{
shutdown_ = false;
thread_ = new boost::thread(boost::bind(&EventLoop::execute, this));
thread_ = new libfreenect2::thread(&EventLoop::static_execute, this);
}
}

Expand Down
6 changes: 3 additions & 3 deletions examples/protonect/src/frame_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ FrameListener::~FrameListener()

void FrameListener::waitForNewFrame(FrameMap &frame)
{
boost::mutex::scoped_lock l(mutex_);
libfreenect2::unique_lock l(mutex_);

while(ready_frame_types_ != subscribed_frame_types_)
{
condition_.wait(l);
WAIT_CONDITION(condition_, mutex_, l)
}

frame = next_frame_;
Expand All @@ -69,7 +69,7 @@ bool FrameListener::addNewFrame(Frame::Type type, Frame *frame)
if((subscribed_frame_types_ & type) == 0) return false;

{
boost::mutex::scoped_lock l(mutex_);
libfreenect2::lock_guard l(mutex_);

FrameMap::iterator it = next_frame_.find(type);

Expand Down
Loading