2009-07-29 12:07:54 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2007-2009 FastMQ Inc.
|
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the Lesser GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
0MQ is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
Lesser GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the Lesser GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-09-16 16:49:09 +02:00
|
|
|
#include "../bindings/c/zmq.h"
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-09-22 11:52:35 +02:00
|
|
|
#include <string.h>
|
2009-07-29 12:07:54 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <new>
|
|
|
|
|
2009-08-09 16:30:22 +02:00
|
|
|
#include "socket_base.hpp"
|
2009-10-01 10:56:17 +02:00
|
|
|
#include "app_thread.hpp"
|
2009-08-08 16:01:58 +02:00
|
|
|
#include "dispatcher.hpp"
|
2009-08-21 14:29:22 +02:00
|
|
|
#include "msg_content.hpp"
|
2009-09-08 11:30:49 +02:00
|
|
|
#include "platform.hpp"
|
|
|
|
#include "stdint.hpp"
|
2009-10-01 10:56:17 +02:00
|
|
|
#include "err.hpp"
|
|
|
|
|
|
|
|
#if defined ZMQ_HAVE_LINUX
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
2009-09-08 11:30:49 +02:00
|
|
|
|
|
|
|
#if !defined ZMQ_HAVE_WINDOWS
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-09-22 11:52:35 +02:00
|
|
|
const char *zmq_strerror (int errnum_)
|
|
|
|
{
|
|
|
|
switch (errnum_) {
|
2009-09-22 12:08:18 +02:00
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
|
|
|
case ENOTSUP:
|
|
|
|
return "Not supported";
|
|
|
|
case EPROTONOSUPPORT:
|
|
|
|
return "Protocol not supported";
|
2009-10-01 13:48:04 +02:00
|
|
|
case ENOBUFS:
|
|
|
|
return "No buffer space available";
|
|
|
|
case ENETDOWN:
|
|
|
|
return "Network is down";
|
|
|
|
case EADDRINUSE:
|
|
|
|
return "Address in use";
|
|
|
|
case EADDRNOTAVAIL:
|
|
|
|
return "Address not available";
|
2009-09-22 12:08:18 +02:00
|
|
|
#endif
|
2009-09-22 11:52:35 +02:00
|
|
|
case EMTHREAD:
|
|
|
|
return "Number of preallocated application threads exceeded";
|
|
|
|
case EFSM:
|
|
|
|
return "Operation cannot be accomplished in current state";
|
|
|
|
case ENOCOMPATPROTO:
|
|
|
|
return "The protocol is not compatible with the socket type";
|
|
|
|
default:
|
2009-09-22 12:08:18 +02:00
|
|
|
#if defined _MSC_VER
|
|
|
|
#pragma warning (push)
|
|
|
|
#pragma warning (disable:4996)
|
|
|
|
#endif
|
2009-09-22 11:52:35 +02:00
|
|
|
return strerror (errnum_);
|
2009-09-22 12:08:18 +02:00
|
|
|
#if defined _MSC_VER
|
|
|
|
#pragma warning (pop)
|
|
|
|
#endif
|
2009-09-22 11:52:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_msg_init (zmq_msg_t *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
msg_->content = (zmq::msg_content_t*) ZMQ_VSM;
|
2009-07-29 12:07:54 +02:00
|
|
|
msg_->vsm_size = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_msg_init_size (zmq_msg_t *msg_, size_t size_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
if (size_ <= ZMQ_MAX_VSM_SIZE) {
|
2009-08-21 14:29:22 +02:00
|
|
|
msg_->content = (zmq::msg_content_t*) ZMQ_VSM;
|
2009-09-08 11:30:49 +02:00
|
|
|
msg_->vsm_size = (uint8_t) size_;
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
else {
|
2009-08-21 14:29:22 +02:00
|
|
|
msg_->content =
|
|
|
|
(zmq::msg_content_t*) malloc (sizeof (zmq::msg_content_t) + size_);
|
2009-07-29 12:07:54 +02:00
|
|
|
if (!msg_->content) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
msg_->shared = 0;
|
2009-08-21 14:29:22 +02:00
|
|
|
|
|
|
|
zmq::msg_content_t *content = (zmq::msg_content_t*) msg_->content;
|
|
|
|
content->data = (void*) (content + 1);
|
|
|
|
content->size = size_;
|
|
|
|
content->ffn = NULL;
|
|
|
|
new (&content->refcnt) zmq::atomic_counter_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_msg_init_data (zmq_msg_t *msg_, void *data_, size_t size_,
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_free_fn *ffn_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
msg_->shared = 0;
|
2009-08-21 14:29:22 +02:00
|
|
|
msg_->content = (zmq::msg_content_t*) malloc (sizeof (zmq::msg_content_t));
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_assert (msg_->content);
|
2009-08-21 14:29:22 +02:00
|
|
|
zmq::msg_content_t *content = (zmq::msg_content_t*) msg_->content;
|
|
|
|
content->data = data_;
|
|
|
|
content->size = size_;
|
|
|
|
content->ffn = ffn_;
|
|
|
|
new (&content->refcnt) zmq::atomic_counter_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_msg_close (zmq_msg_t *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
// For VSMs and delimiters there are no resources to free.
|
|
|
|
if (msg_->content == (zmq::msg_content_t*) ZMQ_DELIMITER ||
|
2009-09-22 10:57:46 +02:00
|
|
|
msg_->content == (zmq::msg_content_t*) ZMQ_VSM)
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
// If the content is not shared, or if it is shared and the reference.
|
2009-07-29 12:07:54 +02:00
|
|
|
// count has dropped to zero, deallocate it.
|
2009-08-21 14:29:22 +02:00
|
|
|
zmq::msg_content_t *content = (zmq::msg_content_t*) msg_->content;
|
|
|
|
if (!msg_->shared || !content->refcnt.sub (1)) {
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
// We used "placement new" operator to initialize the reference.
|
2009-07-29 12:07:54 +02:00
|
|
|
// counter so we call its destructor now.
|
2009-08-21 14:29:22 +02:00
|
|
|
content->refcnt.~atomic_counter_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
if (content->ffn)
|
|
|
|
content->ffn (content->data);
|
|
|
|
free (content);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_msg_move (zmq_msg_t *dest_, zmq_msg_t *src_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_msg_close (dest_);
|
2009-07-29 12:07:54 +02:00
|
|
|
*dest_ = *src_;
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_msg_init (src_);
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_msg_copy (zmq_msg_t *dest_, zmq_msg_t *src_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_msg_close (dest_);
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
// VSMs and delimiters require no special handling.
|
2009-09-22 10:57:46 +02:00
|
|
|
if (src_->content != (zmq::msg_content_t*) ZMQ_DELIMITER &&
|
|
|
|
src_->content != (zmq::msg_content_t*) ZMQ_VSM) {
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
// One reference is added to shared messages. Non-shared messages
|
|
|
|
// are turned into shared messages and reference count is set to 2.
|
2009-08-21 14:29:22 +02:00
|
|
|
zmq::msg_content_t *content = (zmq::msg_content_t*) src_->content;
|
2009-07-29 12:07:54 +02:00
|
|
|
if (src_->shared)
|
2009-08-21 14:29:22 +02:00
|
|
|
content->refcnt.add (1);
|
2009-07-29 12:07:54 +02:00
|
|
|
else {
|
|
|
|
src_->shared = true;
|
2009-08-21 14:29:22 +02:00
|
|
|
content->refcnt.set (2);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest_ = *src_;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
void *zmq_msg_data (zmq_msg_t *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
if (msg_->content == (zmq::msg_content_t*) ZMQ_VSM)
|
2009-07-29 12:07:54 +02:00
|
|
|
return msg_->vsm_data;
|
2009-09-22 10:57:46 +02:00
|
|
|
if (msg_->content == (zmq::msg_content_t*) ZMQ_DELIMITER)
|
2009-07-29 12:07:54 +02:00
|
|
|
return NULL;
|
2009-08-21 14:29:22 +02:00
|
|
|
|
|
|
|
return ((zmq::msg_content_t*) msg_->content)->data;
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
size_t zmq_msg_size (zmq_msg_t *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
if (msg_->content == (zmq::msg_content_t*) ZMQ_VSM)
|
2009-07-29 12:07:54 +02:00
|
|
|
return msg_->vsm_size;
|
2009-09-22 10:57:46 +02:00
|
|
|
if (msg_->content == (zmq::msg_content_t*) ZMQ_DELIMITER)
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
2009-08-21 14:29:22 +02:00
|
|
|
|
|
|
|
return ((zmq::msg_content_t*) msg_->content)->size;
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-09-20 10:14:21 +02:00
|
|
|
void *zmq_init (int app_threads_, int io_threads_, int flags_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-11-21 20:59:55 +01:00
|
|
|
// There should be at least a single application thread managed
|
|
|
|
// by the dispatcher. There's no need for I/O threads if 0MQ is used
|
|
|
|
// only for inproc messaging
|
|
|
|
if (app_threads_ < 1 || io_threads_ < 0 ||
|
2009-09-20 10:14:21 +02:00
|
|
|
app_threads_ > 63 || io_threads_ > 63) {
|
2009-07-29 12:07:54 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
zmq::dispatcher_t *dispatcher = new zmq::dispatcher_t (app_threads_,
|
2009-09-20 10:14:21 +02:00
|
|
|
io_threads_, flags_);
|
2009-08-08 16:01:58 +02:00
|
|
|
zmq_assert (dispatcher);
|
|
|
|
return (void*) dispatcher;
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
int zmq_term (void *dispatcher_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-09-04 16:02:41 +02:00
|
|
|
return ((zmq::dispatcher_t*) dispatcher_)->term ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
void *zmq_socket (void *dispatcher_, int type_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-08 16:01:58 +02:00
|
|
|
return (void*) (((zmq::dispatcher_t*) dispatcher_)->create_socket (type_));
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_close (void *s_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-09 16:30:22 +02:00
|
|
|
((zmq::socket_base_t*) s_)->close ();
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_setsockopt (void *s_, int option_, const void *optval_,
|
|
|
|
size_t optvallen_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-09 16:30:22 +02:00
|
|
|
return (((zmq::socket_base_t*) s_)->setsockopt (option_, optval_,
|
|
|
|
optvallen_));
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-09 09:24:48 +02:00
|
|
|
int zmq_bind (void *s_, const char *addr_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-09 16:30:22 +02:00
|
|
|
return (((zmq::socket_base_t*) s_)->bind (addr_));
|
2009-08-09 09:24:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int zmq_connect (void *s_, const char *addr_)
|
|
|
|
{
|
2009-08-09 16:30:22 +02:00
|
|
|
return (((zmq::socket_base_t*) s_)->connect (addr_));
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_send (void *s_, zmq_msg_t *msg_, int flags_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-09 16:30:22 +02:00
|
|
|
return (((zmq::socket_base_t*) s_)->send (msg_, flags_));
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_flush (void *s_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-09 16:30:22 +02:00
|
|
|
return (((zmq::socket_base_t*) s_)->flush ());
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_recv (void *s_, zmq_msg_t *msg_, int flags_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-09 16:30:22 +02:00
|
|
|
return (((zmq::socket_base_t*) s_)->recv (msg_, flags_));
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
2009-09-08 11:30:49 +02:00
|
|
|
|
2009-10-01 10:56:17 +02:00
|
|
|
int zmq_poll (zmq_pollitem_t *items_, int nitems_)
|
|
|
|
{
|
|
|
|
// TODO: Replace the polling mechanism by the virtualised framework
|
|
|
|
// used in 0MQ I/O threads. That'll make the thing work on all platforms.
|
|
|
|
#if !defined ZMQ_HAVE_LINUX
|
|
|
|
errno = ENOTSUP;
|
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
|
|
|
|
pollfd *pollfds = (pollfd*) malloc (nitems_ * sizeof (pollfd));
|
|
|
|
zmq_assert (pollfds);
|
|
|
|
int npollfds = 0;
|
|
|
|
int nsockets = 0;
|
|
|
|
|
|
|
|
zmq::app_thread_t *app_thread = NULL;
|
|
|
|
|
|
|
|
for (int i = 0; i != nitems_; i++) {
|
|
|
|
|
|
|
|
// 0MQ sockets.
|
|
|
|
if (items_ [i].socket) {
|
|
|
|
|
|
|
|
// Get the app_thread the socket is living in. If there are two
|
|
|
|
// sockets in the same pollset with different app threads, fail.
|
|
|
|
zmq::socket_base_t *s = (zmq::socket_base_t*) items_ [i].socket;
|
|
|
|
if (app_thread) {
|
|
|
|
if (app_thread != s->get_thread ()) {
|
|
|
|
free (pollfds);
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
app_thread = s->get_thread ();
|
|
|
|
|
|
|
|
nsockets++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Raw file descriptors.
|
|
|
|
pollfds [npollfds].fd = items_ [i].fd;
|
|
|
|
pollfds [npollfds].events =
|
|
|
|
(items_ [i].events & ZMQ_POLLIN ? POLLIN : 0) |
|
|
|
|
(items_ [i].events & ZMQ_POLLOUT ? POLLOUT : 0);
|
|
|
|
npollfds++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's at least one 0MQ socket in the pollset we have to poll
|
|
|
|
// for 0MQ commands. If ZMQ_POLL was not set, fail.
|
|
|
|
if (nsockets) {
|
|
|
|
pollfds [npollfds].fd = app_thread->get_signaler ()->get_fd ();
|
|
|
|
if (pollfds [npollfds].fd == zmq::retired_fd) {
|
|
|
|
free (pollfds);
|
|
|
|
errno = ENOTSUP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pollfds [npollfds].events = POLLIN;
|
|
|
|
npollfds++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nevents = 0;
|
|
|
|
bool initial = true;
|
|
|
|
while (!nevents) {
|
|
|
|
|
|
|
|
// Wait for activity. In the first iteration just check for events,
|
|
|
|
// don't wait. Waiting would prevent exiting on any events that may
|
|
|
|
// already be signaled on 0MQ sockets.
|
|
|
|
int rc = poll (pollfds, npollfds, initial ? 0 : -1);
|
|
|
|
if (rc == -1 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
errno_assert (rc >= 0);
|
|
|
|
initial = false;
|
|
|
|
|
|
|
|
// Process 0MQ commands if needed.
|
|
|
|
if (nsockets && pollfds [npollfds -1].revents & POLLIN)
|
|
|
|
app_thread->process_commands (false, false);
|
|
|
|
|
|
|
|
// Check for the events.
|
|
|
|
int pollfd_pos = 0;
|
|
|
|
for (int i = 0; i != nitems_; i++) {
|
|
|
|
|
|
|
|
// If the poll item is a raw file descriptor, simply convert
|
|
|
|
// the events to zmq_pollitem_t-style format.
|
|
|
|
if (!items_ [i].socket) {
|
|
|
|
items_ [i].revents =
|
|
|
|
(pollfds [pollfd_pos].revents & POLLIN ? ZMQ_POLLIN : 0) |
|
|
|
|
(pollfds [pollfd_pos].revents & POLLOUT ? ZMQ_POLLOUT : 0);
|
|
|
|
if (items_ [i].revents)
|
|
|
|
nevents++;
|
|
|
|
pollfd_pos++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The poll item is a 0MQ socket.
|
|
|
|
zmq::socket_base_t *s = (zmq::socket_base_t*) items_ [i].socket;
|
|
|
|
items_ [i].revents = 0;
|
|
|
|
if ((items_ [i].events & ZMQ_POLLOUT) && s->has_out ())
|
|
|
|
items_ [i].revents |= ZMQ_POLLOUT;
|
|
|
|
if ((items_ [i].events & ZMQ_POLLIN) && s->has_in ())
|
|
|
|
items_ [i].revents |= ZMQ_POLLIN;
|
|
|
|
if (items_ [i].revents)
|
|
|
|
nevents++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free (pollfds);
|
|
|
|
return nevents;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-09-08 11:30:49 +02:00
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
|
|
|
|
|
|
|
static uint64_t now ()
|
|
|
|
{
|
|
|
|
// Get the high resolution counter's accuracy.
|
|
|
|
LARGE_INTEGER ticksPerSecond;
|
|
|
|
QueryPerformanceFrequency (&ticksPerSecond);
|
|
|
|
|
|
|
|
// What time is it?
|
|
|
|
LARGE_INTEGER tick;
|
|
|
|
QueryPerformanceCounter (&tick);
|
|
|
|
|
|
|
|
// Convert the tick number into the number of seconds
|
|
|
|
// since the system was started.
|
|
|
|
double ticks_div = (double) (ticksPerSecond.QuadPart / 1000000);
|
|
|
|
return (uint64_t) (tick.QuadPart / ticks_div);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq_sleep (int seconds_)
|
|
|
|
{
|
|
|
|
Sleep (seconds_ * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static uint64_t now ()
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = gettimeofday (&tv, NULL);
|
|
|
|
assert (rc == 0);
|
|
|
|
return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq_sleep (int seconds_)
|
|
|
|
{
|
|
|
|
sleep (seconds_);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void *zmq_stopwatch_start ()
|
|
|
|
{
|
|
|
|
uint64_t *watch = (uint64_t*) malloc (sizeof (uint64_t));
|
|
|
|
zmq_assert (watch);
|
|
|
|
*watch = now ();
|
|
|
|
return (void*) watch;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long zmq_stopwatch_stop (void *watch_)
|
|
|
|
{
|
|
|
|
uint64_t end = now ();
|
|
|
|
uint64_t start = *(uint64_t*) watch_;
|
|
|
|
free (watch_);
|
|
|
|
return (unsigned long) (end - start);
|
2009-09-08 16:55:03 +02:00
|
|
|
}
|
|
|
|
|