From cc3755a16f00026af882ed14d122cc8aa6d50e82 Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Mon, 3 Aug 2009 11:30:13 +0200 Subject: renamed from zs to zmq --- .gitignore | 3 + Makefile.am | 2 +- configure.in | 38 ++++---- examples/chat/Makefile.am | 6 +- examples/chat/chatroom.cpp | 12 +-- examples/chat/display.cpp | 10 +- examples/chat/prompt.cpp | 8 +- include/zmq.h | 207 ++++++++++++++++++++++++++++++++++++++++ include/zmq.hpp | 231 +++++++++++++++++++++++++++++++++++++++++++++ include/zs.h | 206 ---------------------------------------- include/zs.hpp | 231 --------------------------------------------- src/Makefile.am | 10 +- src/app_thread.cpp | 70 +++++++------- src/app_thread.hpp | 8 +- src/atomic.hpp | 62 ++++++------ src/atomic_bitmap.hpp | 82 ++++++++-------- src/atomic_counter.hpp | 72 +++++++------- src/atomic_ptr.hpp | 72 +++++++------- src/command.hpp | 6 +- src/config.hpp | 6 +- src/connecter.cpp | 48 +++++----- src/connecter.hpp | 12 +-- src/data_distributor.cpp | 42 ++++----- src/data_distributor.hpp | 10 +- src/decoder.hpp | 6 +- src/devpoll.cpp | 38 ++++---- src/devpoll.hpp | 8 +- src/dispatcher.cpp | 52 +++++----- src/dispatcher.hpp | 8 +- src/dummy_aggregator.cpp | 34 +++---- src/dummy_aggregator.hpp | 8 +- src/dummy_distributor.cpp | 28 +++--- src/dummy_distributor.hpp | 8 +- src/encoder.hpp | 6 +- src/epoll.cpp | 36 +++---- src/epoll.hpp | 8 +- src/err.cpp | 8 +- src/err.hpp | 16 ++-- src/fair_aggregator.cpp | 30 +++--- src/fair_aggregator.hpp | 8 +- src/fd.hpp | 8 +- src/fd_signaler.cpp | 54 +++++------ src/fd_signaler.hpp | 8 +- src/i_api.hpp | 14 +-- src/i_demux.hpp | 11 ++- src/i_engine.hpp | 8 +- src/i_mux.hpp | 11 ++- src/i_poll_events.hpp | 6 +- src/i_poller.hpp | 6 +- src/i_session.hpp | 10 +- src/i_signaler.hpp | 6 +- src/i_thread.hpp | 6 +- src/io_object.cpp | 6 +- src/io_object.hpp | 6 +- src/io_thread.cpp | 72 +++++++------- src/io_thread.hpp | 8 +- src/ip.cpp | 36 +++---- src/ip.hpp | 8 +- src/kqueue.cpp | 40 ++++---- src/kqueue.hpp | 8 +- src/listener.cpp | 42 ++++----- src/listener.hpp | 6 +- src/load_balancer.cpp | 32 +++---- src/load_balancer.hpp | 10 +- src/msg.hpp | 16 ++-- src/mutex.hpp | 10 +- src/object.cpp | 88 ++++++++--------- src/object.hpp | 6 +- src/p2p.cpp | 4 +- src/p2p.hpp | 6 +- src/pipe.cpp | 14 +-- src/pipe.hpp | 10 +- src/pipe_reader.cpp | 26 ++--- src/pipe_reader.hpp | 8 +- src/pipe_writer.cpp | 30 +++--- src/pipe_writer.hpp | 8 +- src/platform.hpp.in | 26 ++--- src/poll.cpp | 40 ++++---- src/poll.hpp | 14 +-- src/pub.cpp | 6 +- src/pub.hpp | 8 +- src/rep.cpp | 4 +- src/rep.hpp | 6 +- src/req.cpp | 4 +- src/req.hpp | 6 +- src/safe_object.cpp | 14 +-- src/safe_object.hpp | 6 +- src/select.cpp | 40 ++++---- src/select.hpp | 10 +- src/session.cpp | 46 ++++----- src/session.hpp | 10 +- src/session_stub.cpp | 30 +++--- src/session_stub.hpp | 10 +- src/simple_semaphore.hpp | 12 +-- src/socket_base.cpp | 56 +++++------ src/socket_base.hpp | 14 +-- src/stdint.hpp | 6 +- src/sub.cpp | 8 +- src/sub.hpp | 8 +- src/tcp_connecter.cpp | 20 ++-- src/tcp_connecter.hpp | 6 +- src/tcp_listener.cpp | 20 ++-- src/tcp_listener.hpp | 6 +- src/tcp_socket.cpp | 18 ++-- src/tcp_socket.hpp | 6 +- src/thread.cpp | 14 +-- src/thread.hpp | 10 +- src/uuid.cpp | 40 ++++---- src/uuid.hpp | 18 ++-- src/windows.hpp | 4 +- src/wire.hpp | 6 +- src/ypipe.hpp | 8 +- src/ypollset.cpp | 10 +- src/ypollset.hpp | 6 +- src/yqueue.hpp | 10 +- src/zmq.cpp | 223 +++++++++++++++++++++++++++++++++++++++++++ src/zmq_decoder.cpp | 25 ++--- src/zmq_decoder.hpp | 10 +- src/zmq_encoder.cpp | 18 ++-- src/zmq_encoder.hpp | 10 +- src/zmq_tcp_engine.cpp | 44 ++++----- src/zmq_tcp_engine.hpp | 6 +- src/zs.cpp | 222 ------------------------------------------- 123 files changed, 1778 insertions(+), 1770 deletions(-) create mode 100644 include/zmq.h create mode 100644 include/zmq.hpp delete mode 100644 include/zs.h delete mode 100644 include/zs.hpp create mode 100644 src/zmq.cpp delete mode 100644 src/zs.cpp diff --git a/.gitignore b/.gitignore index e69de29..cf19bd8 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +*.lo +*.la diff --git a/Makefile.am b/Makefile.am index cc77307..156c97e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,4 +1,4 @@ -include_HEADERS = include/zs.h include/zs.hpp +include_HEADERS = include/zmq.h include/zmq.hpp SUBDIRS = src examples DIST_SUBDIRS = src examples diff --git a/configure.in b/configure.in index e75ab11..932b4dd 100644 --- a/configure.in +++ b/configure.in @@ -1,7 +1,7 @@ # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ(2.61) -AC_INIT([zsock],[dev]) +AC_INIT([zeromq],[dev]) AC_CONFIG_AUX_DIR(config) AM_CONFIG_HEADER(src/platform.hpp) AM_INIT_AUTOMAKE(AC_PACKAGE_NAME, AC_PACKAGE_VERSION) @@ -20,7 +20,7 @@ AC_CANONICAL_HOST case "${host_os}" in *linux*) - AC_DEFINE(ZS_HAVE_LINUX, 1, [Have Linux OS]) + AC_DEFINE(ZMQ_HAVE_LINUX, 1, [Have Linux OS]) CPPFLAGS="-D_REENTRANT $CPPFLAGS" sed < libtool > libtool-2 \ 's/^hardcode_libdir_flag_spec.*$'/'hardcode_libdir_flag_spec=" "/' @@ -29,7 +29,7 @@ case "${host_os}" in AC_CHECK_LIB(uuid, uuid_generate) ;; *solaris*) - AC_DEFINE(ZS_HAVE_SOLARIS, 1, [Have Solaris OS]) + AC_DEFINE(ZMQ_HAVE_SOLARIS, 1, [Have Solaris OS]) AC_CHECK_LIB(socket, main) AC_CHECK_LIB(nsl, main) AC_CHECK_LIB(rt, main) @@ -45,37 +45,37 @@ case "${host_os}" in AC_MSG_RESULT([$solaris_has_atomic]) # Solaris 8 does not have atomic operations exported to user space. if test "x$solaris_has_atomic" = "xno"; then - AC_DEFINE(ZS_FORCE_MUTEXES, 1, [Force to use mutexes]) + AC_DEFINE(ZMQ_FORCE_MUTEXES, 1, [Force to use mutexes]) fi ;; *freebsd*) - AC_DEFINE(ZS_HAVE_FREEBSD, 1, [Have FreeBSD OS]) + AC_DEFINE(ZMQ_HAVE_FREEBSD, 1, [Have FreeBSD OS]) CPPFLAGS="-D_THREAD_SAFE $CPPFLAGS" LIBS="-pthread" ;; *darwin*) - AC_DEFINE(ZS_HAVE_OSX, 1, [Have DarwinOSX OS]) + AC_DEFINE(ZMQ_HAVE_OSX, 1, [Have DarwinOSX OS]) LIBS="-pthread" - ZS_EXTRA_CXXFLAGS+="-Wno-uninitialized" + ZMQ_EXTRA_CXXFLAGS+="-Wno-uninitialized" ;; *openbsd*) - AC_DEFINE(ZS_HAVE_OPENBSD, 1, [Have OpenBSD OS]) + AC_DEFINE(ZMQ_HAVE_OPENBSD, 1, [Have OpenBSD OS]) CPPFLAGS="-pthread $CPPFLAGS" LIBS="-pthread" ;; *nto-qnx*) - AC_DEFINE(ZS_HAVE_QNXNTO, 1, [Have QNX Neutrino OS]) + AC_DEFINE(ZMQ_HAVE_QNXNTO, 1, [Have QNX Neutrino OS]) CPPFLAGS="-D_THREAD_SAFE $CPPFLAGS" AC_CHECK_LIB(socket,main) ;; *aix*) - AC_DEFINE(ZS_HAVE_AIX, 1, [Have AIX OS]) + AC_DEFINE(ZMQ_HAVE_AIX, 1, [Have AIX OS]) if test "x$GXX" = "xyes"; then CPPFLAGS="-D_THREAD_SAFE $CPPFLAGS" fi ;; *hpux*) - AC_DEFINE(ZS_HAVE_HPUX, 1, [Have HPUX OS]) + AC_DEFINE(ZMQ_HAVE_HPUX, 1, [Have HPUX OS]) if test "x$GXX" = "xyes"; then CPPFLAGS="-D_THREAD_SAFE $CPPFLAGS" fi @@ -86,8 +86,8 @@ case "${host_os}" in chmod 755 libtool ;; *mingw32*) - AC_DEFINE(ZS_HAVE_WINDOWS, 1, [Have Windows OS]) - AC_DEFINE(ZS_HAVE_MINGW32, 1, [Have MinGW32]) + AC_DEFINE(ZMQ_HAVE_WINDOWS, 1, [Have Windows OS]) + AC_DEFINE(ZMQ_HAVE_MINGW32, 1, [Have MinGW32]) AC_CHECK_HEADERS(windows.h) LIBS="-lwsock32 -lws2_32 -no-undefined" CFLAGS="-std=c99" @@ -122,10 +122,10 @@ AC_CHECK_HEADERS(errno.h arpa/inet.h netinet/tcp.h netinet/in.h stddef.h \ stdlib.h string.h sys/socket.h sys/time.h unistd.h limits.h) # Check if we have eventfd.h header file. -AC_CHECK_HEADERS(sys/eventfd.h, [AC_DEFINE(ZS_HAVE_EVENTFD, 1, [Have eventfd extension.])]) +AC_CHECK_HEADERS(sys/eventfd.h, [AC_DEFINE(ZMQ_HAVE_EVENTFD, 1, [Have eventfd extension.])]) # Check if we have ifaddrs.h header file. -AC_CHECK_HEADERS(ifaddrs.h, [AC_DEFINE(ZS_HAVE_IFADDRS, 1, [Have ifaddrs.h header.])]) +AC_CHECK_HEADERS(ifaddrs.h, [AC_DEFINE(ZMQ_HAVE_IFADDRS, 1, [Have ifaddrs.h header.])]) # Use c++ in subsequent tests AC_LANG(C++) @@ -162,8 +162,8 @@ fi AC_SUBST(stdint) AC_SUBST(inttypes) -# Subst ZS_EXTRA_CXXFLAGS -AC_SUBST(ZS_EXTRA_CXXFLAGS) +# Subst ZMQ_EXTRA_CXXFLAGS +AC_SUBST(ZMQ_EXTRA_CXXFLAGS) # Checks for library functions. @@ -175,7 +175,7 @@ AC_OUTPUT(Makefile src/Makefile examples/Makefile examples/chat/Makefile) AC_MSG_RESULT([]) AC_MSG_RESULT([ ******************************************************** ]) -AC_MSG_RESULT([ 0SOCKETS ]) +AC_MSG_RESULT([ 0MQ ]) AC_MSG_RESULT([ ******************************************************** ]) AC_MSG_RESULT([ This software is distributed under the terms and ]) AC_MSG_RESULT([ conditions of the LESSER GNU GENERAL PUBLIC LICENSE. ]) @@ -183,6 +183,6 @@ AC_MSG_RESULT([ See the file COPYING and COPYING.LESSER for the full ]) AC_MSG_RESULT([ license text. ]) AC_MSG_RESULT([ ******************************************************** ]) AC_MSG_RESULT([]) -AC_MSG_RESULT([ zsock install dir: $prefix]) +AC_MSG_RESULT([ 0MQ install dir: $prefix]) AC_MSG_RESULT([]) diff --git a/examples/chat/Makefile.am b/examples/chat/Makefile.am index afdb827..84dad79 100644 --- a/examples/chat/Makefile.am +++ b/examples/chat/Makefile.am @@ -3,13 +3,13 @@ INCLUDES = -I$(top_builddir) -I$(top_builddir)/include noinst_PROGRAMS = chatroom display prompt chatroom_SOURCES = chatroom.cpp -chatroom_LDADD = $(top_builddir)/src/libzs.la +chatroom_LDADD = $(top_builddir)/src/libzmq.la chatroom_CXXFLAGS = -Wall -pedantic -Werror display_SOURCES = display.cpp -display_LDADD = $(top_builddir)/src/libzs.la +display_LDADD = $(top_builddir)/src/libzmq.la display_CXXFLAGS = -Wall -pedantic -Werror prompt_SOURCES = prompt.cpp -prompt_LDADD = $(top_builddir)/src/libzs.la +prompt_LDADD = $(top_builddir)/src/libzmq.la prompt_CXXFLAGS = -Wall -pedantic -Werror diff --git a/examples/chat/chatroom.cpp b/examples/chat/chatroom.cpp index f2240ab..4cf123a 100644 --- a/examples/chat/chatroom.cpp +++ b/examples/chat/chatroom.cpp @@ -23,7 +23,7 @@ using namespace std; -#include +#include int main (int argc, const char *argv []) { @@ -38,19 +38,19 @@ int main (int argc, const char *argv []) const char *out_interface = argv [2]; // Initialise 0MQ infrastructure - zs::context_t ctx (1, 1); + zmq::context_t ctx (1, 1); // Create two sockets. One for receiving messages from 'propmt' // applications, one for sending messages to 'display' applications - zs::socket_t in_socket (ctx, ZS_SUB); + zmq::socket_t in_socket (ctx, ZMQ_SUB); in_socket.bind (in_interface); - zs::socket_t out_socket (ctx, ZS_PUB); + zmq::socket_t out_socket (ctx, ZMQ_PUB); out_socket.bind (out_interface); while (true) { // Get a message - zs::message_t in_message; + zmq::message_t in_message; in_socket.recv (&in_message); // Get the current time. Replace the newline character at the end @@ -62,7 +62,7 @@ int main (int argc, const char *argv []) timebuf [strlen (timebuf) - 1] = ' '; // Create and fill in the message - zs::message_t out_message (strlen (timebuf) + in_message.size ()); + zmq::message_t out_message (strlen (timebuf) + in_message.size ()); char *data = (char*) out_message.data (); memcpy (data, timebuf, strlen (timebuf)); data += strlen (timebuf); diff --git a/examples/chat/display.cpp b/examples/chat/display.cpp index ceb096f..3da565e 100644 --- a/examples/chat/display.cpp +++ b/examples/chat/display.cpp @@ -23,7 +23,7 @@ using namespace std; -#include +#include int main (int argc, const char *argv []) { @@ -38,17 +38,17 @@ int main (int argc, const char *argv []) // Initialise 0MQ infrastructure, connect to the chatroom and ask for all // messages and gap notifications. - zs::context_t ctx (1, 1); - zs::socket_t s (ctx, ZS_SUB); + zmq::context_t ctx (1, 1); + zmq::socket_t s (ctx, ZMQ_SUB); s.connect (chatroom_out_address); s.subscribe ("*"); while (true) { // Get a message and print it to the console. - zs::message_t message; + zmq::message_t message; s.recv (&message); - if (message.type () == zs::message_gap) + if (message.type () == zmq::message_gap) cout << "Problems connecting to the chatroom..." << endl; else cout << (char*) message.data () << flush; diff --git a/examples/chat/prompt.cpp b/examples/chat/prompt.cpp index 461e7b8..66ceaf4 100644 --- a/examples/chat/prompt.cpp +++ b/examples/chat/prompt.cpp @@ -23,7 +23,7 @@ using namespace std; -#include +#include int main (int argc, const char *argv []) { @@ -38,8 +38,8 @@ int main (int argc, const char *argv []) const char *user_name = argv [2]; // Initialise 0MQ infrastructure and connect to the chatroom. - zs::context_t ctx (1, 1); - zs::socket_t s (ctx, ZS_PUB); + zmq::context_t ctx (1, 1); + zmq::socket_t s (ctx, ZMQ_PUB); s.connect (chatroom_in_address); while (true) { @@ -52,7 +52,7 @@ int main (int argc, const char *argv []) text = text + ": " + textbuf; // Create the message (terminating zero is part of the message) - zs::message_t message (text.size () + 1); + zmq::message_t message (text.size () + 1); memcpy (message.data (), text.c_str (), text.size () + 1); // Send the message diff --git a/include/zmq.h b/include/zmq.h new file mode 100644 index 0000000..0fbc142 --- /dev/null +++ b/include/zmq.h @@ -0,0 +1,207 @@ +/* + 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 . +*/ + +#ifndef __ZMQ_H_INCLUDED__ +#define __ZMQ_H_INCLUDED__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#if defined MSC_VER && defined ZMQ_BUILDING_LIBZMQ +#define ZMQ_EXPORT __declspec(dllexport) +#else +#define ZMQ_EXPORT +#endif + +// Maximal size of "Very Small Message". VSMs are passed by value +// to avoid excessive memory allocation/deallocation. +#define ZMQ_MAX_VSM_SIZE 30 + +// Message & notification types. +#define ZMQ_GAP 1 +#define ZMQ_DELIMITER 31 +#define ZMQ_VSM 32 + +// The operation should be performed in non-blocking mode. I.e. if it cannot +// be processed immediately, error should be returned with errno set to EAGAIN. +#define ZMQ_NOBLOCK 1 + +// zmq_send should not flush the message downstream immediately. Instead, it +// should batch ZMQ_NOFLUSH messages and send them downstream only if zmq_flush +// is invoked. This is an optimisation for cases where several messages are +// sent in a single business transaction. However, the effect is measurable +// only in extremely high-perf scenarios (million messages a second or so). +// If that's not your case, use standard flushing send instead. See exchange +// example for illustration of ZMQ_NOFLUSH functionality. +#define ZMQ_NOFLUSH 2 + +// Socket to communicate with a single peer. Allows for a singe connect or a +// single accept. There's no message routing or message filtering involved. +#define ZMQ_P2P 0 + +// Socket to distribute data. Recv fuction is not implemeted for this socket +// type. Messages are distributed in fanout fashion to all peers. +#define ZMQ_PUB 1 + +// Socket to subscribe to distributed data. Send function is not implemented +// for this socket type. However, subscribe function can be used to modify the +// message filter. +#define ZMQ_SUB 2 + +// Socket to send requests on and receive replies from. Requests are +// load-balanced among all the peers. This socket type doesn't allow for more +// recv's that there were send's. +#define ZMQ_REQ 3 + +// Socket to receive requests from and send replies to. This socket type allows +// only an alternated sequence of recv's and send's. Each send is routed to +// the peer that the previous recv delivered message from. +#define ZMQ_REP 4 + +// Prototype for the message body deallocation functions. +// It is deliberately defined in the way to comply with standard C free. +typedef void (zmq_free_fn) (void *data); + +// A message. If 'shared' is true, message content pointed to by 'content' +// is shared, i.e. reference counting is used to manage its lifetime +// rather than straighforward malloc/free. struct zmq_msg_content is +// not declared in the API. +struct zmq_msg +{ + struct zmq_msg_content *content; + unsigned char shared; + uint16_t vsm_size; + unsigned char vsm_data [ZMQ_MAX_VSM_SIZE]; +}; + +// TODO: Different options... +struct zmq_opts +{ + uint64_t hwm; + uint64_t lwm; + uint64_t swap; + uint64_t mask; + uint64_t taskset; + const char *identity; + const char *args; +}; + +// Initialise an empty message (zero bytes long). +ZMQ_EXPORT int zmq_msg_init (zmq_msg *msg); + +// Initialise a message 'size' bytes long. +// +// Errors: ENOMEM - the size is too large to allocate. +ZMQ_EXPORT int zmq_msg_init_size (zmq_msg *msg, size_t size); + +// Initialise a message from an existing buffer. Message isn't copied, +// instead 0SOCKETS infrastructure take ownership of the buffer and call +// deallocation functio (ffn) once it's not needed anymore. +ZMQ_EXPORT int zmq_msg_init_data (zmq_msg *msg, void *data, size_t size, + zmq_free_fn *ffn); + +// Deallocate the message. +ZMQ_EXPORT int zmq_msg_close (zmq_msg *msg); + +// Move the content of the message from 'src' to 'dest'. The content isn't +// copied, just moved. 'src' is an empty message after the call. Original +// content of 'dest' message is deallocated. +ZMQ_EXPORT int zmq_msg_move (zmq_msg *dest, zmq_msg *src); + +// Copy the 'src' message to 'dest'. The content isn't copied, instead +// reference count is increased. Don't modify the message data after the +// call as they are shared between two messages. Original content of 'dest' +// message is deallocated. +ZMQ_EXPORT int zmq_msg_copy (zmq_msg *dest, zmq_msg *src); + +// Returns pointer to message data. +ZMQ_EXPORT void *zmq_msg_data (zmq_msg *msg); + +// Return size of message data (in bytes). +ZMQ_EXPORT size_t zmq_msg_size (zmq_msg *msg); + +// Returns type of the message. +ZMQ_EXPORT int zmq_msg_type (zmq_msg *msg); + +// Initialise 0SOCKETS context. 'app_threads' specifies maximal number +// of application threads that can have open sockets at the same time. +// 'io_threads' specifies the size of thread pool to handle I/O operations. +// +// Errors: EINVAL - one of the arguments is less than zero or there are no +// threads declared at all. +ZMQ_EXPORT void *zmq_init (int app_threads, int io_threads); + +// Deinitialise 0SOCKETS context including all the open sockets. Closing +// sockets after zmq_term has been called will result in undefined behaviour. +ZMQ_EXPORT int zmq_term (void *context); + +// Open a socket. +// +// Errors: EINVAL - invalid socket type. +// EMFILE - the number of application threads entitled to hold open +// sockets at the same time was exceeded. +ZMQ_EXPORT void *zmq_socket (void *context, int type); + +// Close the socket. +ZMQ_EXPORT int zmq_close (void *s); + +// Bind the socket to a particular address. +ZMQ_EXPORT int zmq_bind (void *s, const char *addr, zmq_opts *opts); + +// Connect the socket to a particular address. +ZMQ_EXPORT int zmq_connect (void *s, const char *addr, zmq_opts *opts); + +// Subscribe for the subset of messages identified by 'criteria' argument. +ZMQ_EXPORT int zmq_subscribe (void *s, const char *criteria); + +// Send the message 'msg' to the socket 's'. 'flags' argument can be +// combination of following values: +// ZMQ_NOBLOCK - if message cannot be sent, return immediately. +// ZMQ_NOFLUSH - message won't be sent immediately. It'll be sent with either +// subsequent flushing send or explicit call to zmq_flush +// function. +// +// Errors: EAGAIN - message cannot be sent at the moment (applies only to +// non-blocking send). +// ENOTSUP - function isn't supported by particular socket type. +ZMQ_EXPORT int zmq_send (void *s, zmq_msg *msg, int flags); + +// Flush the messages that were send using ZMQ_NOFLUSH flag down the stream. +// +// Errors: ENOTSUP - function isn't supported by particular socket type. +ZMQ_EXPORT int zmq_flush (void *s); + +// Send a message from the socket 's'. 'flags' argument can be combination +// of following values: +// ZMQ_NOBLOCK - if message cannot be received, return immediately. +// +// Errors: EAGAIN - message cannot be received at the moment (applies only to +// non-blocking receive). +// ENOTSUP - function isn't supported by particular socket type. +ZMQ_EXPORT int zmq_recv (void *s, zmq_msg *msg, int flags); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/zmq.hpp b/include/zmq.hpp new file mode 100644 index 0000000..317fddf --- /dev/null +++ b/include/zmq.hpp @@ -0,0 +1,231 @@ +/* + 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 . +*/ + +#ifndef __ZMQ_HPP_INCLUDED__ +#define __ZMQ_HPP_INCLUDED__ + +#include "zmq.h" + +#include + +namespace zmq +{ + + typedef zmq_free_fn free_fn; + + enum message_type_t + { + message_data = 1 << 0, + message_gap = 1 << ZMQ_GAP, + message_delimiter = 1 << ZMQ_DELIMITER + }; + + // A message. Caution: Don't change the body of the message once you've + // copied it - the behaviour is undefined. Don't change the body of the + // received message either - other threads may be accessing it in parallel. + + class message_t : private zmq_msg + { + friend class socket_t; + + public: + + // Creates message size_ bytes long. + inline message_t (size_t size_ = 0) + { + int rc = zmq_msg_init_size (this, size_); + assert (rc == 0); + } + + // Creates message from the supplied buffer. 0MQ takes care of + // deallocating the buffer once it is not needed. The deallocation + // function is supplied in ffn_ parameter. If ffn_ is NULL, no + // deallocation happens - this is useful for sending static buffers. + inline message_t (void *data_, size_t size_, + free_fn *ffn_) + { + int rc = zmq_msg_init_data (this, data_, size_, ffn_); + assert (rc == 0); + } + + // Destroys the message. + inline ~message_t () + { + int rc = zmq_msg_close (this); + assert (rc == 0); + } + + // Destroys old content of the message and allocates buffer for the + // new message body. Having this as a separate function allows user + // to reuse once-allocated message for multiple times. + inline void rebuild (size_t size_) + { + int rc = zmq_msg_close (this); + assert (rc == 0); + rc = zmq_msg_init_size (this, size_); + assert (rc == 0); + } + + // Same as above, however, the message is rebuilt from the supplied + // buffer. See appropriate constructor for discussion of buffer + // deallocation mechanism. + inline void rebuild (void *data_, size_t size_, free_fn *ffn_) + { + int rc = zmq_msg_close (this); + assert (rc == 0); + rc = zmq_msg_init_data (this, data_, size_, ffn_); + assert (rc == 0); + } + + // Moves the message content from one message to the another. If the + // destination message have contained data prior to the operation + // these get deallocated. The source message will contain 0 bytes + // of data after the operation. + inline void move_to (message_t *msg_) + { + int rc = zmq_msg_move (this, (zmq_msg*) msg_); + assert (rc == 0); + } + + // Copies the message content from one message to the another. If the + // destination message have contained data prior to the operation + // these get deallocated. + inline void copy_to (message_t *msg_) + { + int rc = zmq_msg_copy (this, (zmq_msg*) msg_); + assert (rc == 0); + } + + // Returns message type. + inline message_type_t type () + { + return (message_type_t) (1 << zmq_msg_type (this)); + } + + // Returns pointer to message's data buffer. + inline void *data () + { + return zmq_msg_data (this); + } + + // Returns the size of message data buffer. + inline size_t size () + { + return zmq_msg_size (this); + } + + private: + + // Disable implicit message copying, so that users won't use shared + // messages (less efficient) without being aware of the fact. + message_t (const message_t&); + void operator = (const message_t&); + }; + + class context_t + { + friend class socket_t; + + public: + + inline context_t (int app_threads_, int io_threads_) + { + ptr = zmq_init (app_threads_, io_threads_); + assert (ptr); + } + + inline ~context_t () + { + int rc = zmq_term (ptr); + assert (rc == 0); + } + + private: + + void *ptr; + + // Disable copying. + context_t (const context_t&); + void operator = (const context_t&); + }; + + class socket_t + { + public: + + inline socket_t (context_t &context_, int type_ = 0) + { + ptr = zmq_socket (context_.ptr, type_); + assert (ptr); + } + + inline ~socket_t () + { + int rc = zmq_close (ptr); + assert (rc == 0); + } + + inline void bind (const char *addr_, zmq_opts *opts_ = NULL) + { + int rc = zmq_bind (ptr, addr_, opts_); + assert (rc == 0); + } + + inline void connect (const char *addr_, zmq_opts *opts_ = NULL) + { + int rc = zmq_connect (ptr, addr_, opts_); + assert (rc == 0); + } + + inline void subscribe (const char *criteria_) + { + int rc = zmq_subscribe (ptr, criteria_); + assert (rc == 0); + } + + inline void send (message_t &msg_, int flags_ = 0) + { + int rc = zmq_send (ptr, &msg_, flags_); + assert (rc == 0); + } + + inline void flush () + { + int rc = zmq_flush (ptr); + assert (rc == 0); + } + + inline void recv (message_t *msg_, int flags_ = 0) + { + int rc = zmq_recv (ptr, msg_, flags_); + assert (rc == 0); + } + + private: + + void *ptr; + + // Disable copying. + socket_t (const socket_t&); + void operator = (const socket_t&); + }; + +} + +#endif diff --git a/include/zs.h b/include/zs.h deleted file mode 100644 index cae1a17..0000000 --- a/include/zs.h +++ /dev/null @@ -1,206 +0,0 @@ -/* - 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 . -*/ - -#ifndef __ZSOCKETS_H_INCLUDED__ -#define __ZSOCKETS_H_INCLUDED__ - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -#if defined MSC_VER && defined ZS_BUILDING_LIBZS -#define ZS_EXPORT __declspec(dllexport) -#else -#define ZS_EXPORT -#endif - -// Maximal size of "Very Small Message". VSMs are passed by value -// to avoid excessive memory allocation/deallocation. -#define ZS_MAX_VSM_SIZE 30 - -// Message & notification types. -#define ZS_GAP 1 -#define ZS_DELIMITER 31 -#define ZS_VSM 32 - -// The operation should be performed in non-blocking mode. I.e. if it cannot -// be processed immediately, error should be returned with errno set to EAGAIN. -#define ZS_NOBLOCK 1 - -// zs_send should not flush the message downstream immediately. Instead, it -// should batch ZS_NOFLUSH messages and send them downstream only when zs_flush -// is invoked. This is an optimisation for cases where several messages are -// sent in a single business transaction. However, the effect is measurable -// only in extremely high-perf scenarios (million messages a second or so). -// If that's not your case, use standard flushing send instead. See exchange -// example for illustration of ZS_NOFLUSH functionality. -#define ZS_NOFLUSH 2 - -// Socket to communicate with a single peer. Allows for a singe connect or a -// single accept. There's no message routing or message filtering involved. -#define ZS_P2P 0 - -// Socket to distribute data. Recv fuction is not implemeted for this socket -// type. Messages are distributed in fanout fashion to all peers. -#define ZS_PUB 1 - -// Socket to subscribe to distributed data. Send function is not implemented -// for this socket type. However, subscribe function can be used to modify the -// message filter. -#define ZS_SUB 2 - -// Socket to send requests on and receive replies from. Requests are -// load-balanced among all the peers. This socket type doesn't allow for more -// recv's that there were send's. -#define ZS_REQ 3 - -// Socket to receive requests from and send replies to. This socket type allows -// only an alternated sequence of recv's and send's. Each send is routed to -// the peer that the previous recv delivered message from. -#define ZS_REP 4 - -// Prototype for the message body deallocation functions. -// It is deliberately defined in the way to comply with standard C free. -typedef void (zs_free_fn) (void *data); - -// A message. If 'shared' is true, message content pointed to by 'content' -// is shared, i.e. reference counting is used to manage its lifetime -// rather than straighforward malloc/free. struct zs_msg_content is -// not declared in the API. -struct zs_msg -{ - struct zs_msg_content *content; - unsigned char shared; - uint16_t vsm_size; - unsigned char vsm_data [ZS_MAX_VSM_SIZE]; -}; - -// TODO: Different options... -struct zs_opts -{ - uint64_t hwm; - uint64_t lwm; - uint64_t swap; - uint64_t mask; - uint64_t taskset; - const char *identity; - const char *args; -}; - -// Initialise an empty message (zero bytes long). -ZS_EXPORT int zs_msg_init (zs_msg *msg); - -// Initialise a message 'size' bytes long. -// -// Errors: ENOMEM - the size is too large to allocate. -ZS_EXPORT int zs_msg_init_size (zs_msg *msg, size_t size); - -// Initialise a message from an existing buffer. Message isn't copied, -// instead 0SOCKETS infrastructure take ownership of the buffer and call -// deallocation functio (ffn) once it's not needed anymore. -ZS_EXPORT int zs_msg_init_data (zs_msg *msg, void *data, size_t size, - zs_free_fn *ffn); - -// Deallocate the message. -ZS_EXPORT int zs_msg_close (zs_msg *msg); - -// Move the content of the message from 'src' to 'dest'. The content isn't -// copied, just moved. 'src' is an empty message after the call. Original -// content of 'dest' message is deallocated. -ZS_EXPORT int zs_msg_move (zs_msg *dest, zs_msg *src); - -// Copy the 'src' message to 'dest'. The content isn't copied, instead -// reference count is increased. Don't modify the message data after the -// call as they are shared between two messages. Original content of 'dest' -// message is deallocated. -ZS_EXPORT int zs_msg_copy (zs_msg *dest, zs_msg *src); - -// Returns pointer to message data. -ZS_EXPORT void *zs_msg_data (zs_msg *msg); - -// Return size of message data (in bytes). -ZS_EXPORT size_t zs_msg_size (zs_msg *msg); - -// Returns type of the message. -ZS_EXPORT int zs_msg_type (zs_msg *msg); - -// Initialise 0SOCKETS context. 'app_threads' specifies maximal number -// of application threads that can have open sockets at the same time. -// 'io_threads' specifies the size of thread pool to handle I/O operations. -// -// Errors: EINVAL - one of the arguments is less than zero or there are no -// threads declared at all. -ZS_EXPORT void *zs_init (int app_threads, int io_threads); - -// Deinitialise 0SOCKETS context including all the open sockets. Closing -// sockets after zs_term has been called will result in undefined behaviour. -ZS_EXPORT int zs_term (void *context); - -// Open a socket. -// -// Errors: EINVAL - invalid socket type. -// EMFILE - the number of application threads entitled to hold open -// sockets at the same time was exceeded. -ZS_EXPORT void *zs_socket (void *context, int type); - -// Close the socket. -ZS_EXPORT int zs_close (void *s); - -// Bind the socket to a particular address. -ZS_EXPORT int zs_bind (void *s, const char *addr, zs_opts *opts); - -// Connect the socket to a particular address. -ZS_EXPORT int zs_connect (void *s, const char *addr, zs_opts *opts); - -// Subscribe for the subset of messages identified by 'criteria' argument. -ZS_EXPORT int zs_subscribe (void *s, const char *criteria); - -// Send the message 'msg' to the socket 's'. 'flags' argument can be -// combination of following values: -// ZS_NOBLOCK - if message cannot be sent, return immediately. -// ZS_NOFLUSH - message won't be sent immediately. It'll be sent with either -// subsequent flushing send or explicit call to zs_flush function. -// -// Errors: EAGAIN - message cannot be sent at the moment (applies only to -// non-blocking send). -// ENOTSUP - function isn't supported by particular socket type. -ZS_EXPORT int zs_send (void *s, zs_msg *msg, int flags); - -// Flush the messages that were send using ZS_NOFLUSH flag down the stream. -// -// Errors: ENOTSUP - function isn't supported by particular socket type. -ZS_EXPORT int zs_flush (void *s); - -// Send a message from the socket 's'. 'flags' argument can be combination -// of following values: -// ZS_NOBLOCK - if message cannot be received, return immediately. -// -// Errors: EAGAIN - message cannot be received at the moment (applies only to -// non-blocking receive). -// ENOTSUP - function isn't supported by particular socket type. -ZS_EXPORT int zs_recv (void *s, zs_msg *msg, int flags); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/include/zs.hpp b/include/zs.hpp deleted file mode 100644 index d0f607f..0000000 --- a/include/zs.hpp +++ /dev/null @@ -1,231 +0,0 @@ -/* - 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 . -*/ - -#ifndef __ZSOCKETS_HPP_INCLUDED__ -#define __ZSOCKETS_HPP_INCLUDED__ - -#include "zs.h" - -#include - -namespace zs -{ - - typedef zs_free_fn free_fn; - - enum message_type_t - { - message_data = 1 << 0, - message_gap = 1 << ZS_GAP, - message_delimiter = 1 << ZS_DELIMITER - }; - - // A message. Caution: Don't change the body of the message once you've - // copied it - the behaviour is undefined. Don't change the body of the - // received message either - other threads may be accessing it in parallel. - - class message_t : private zs_msg - { - friend class socket_t; - - public: - - // Creates message size_ bytes long. - inline message_t (size_t size_ = 0) - { - int rc = zs_msg_init_size (this, size_); - assert (rc == 0); - } - - // Creates message from the supplied buffer. 0MQ takes care of - // deallocating the buffer once it is not needed. The deallocation - // function is supplied in ffn_ parameter. If ffn_ is NULL, no - // deallocation happens - this is useful for sending static buffers. - inline message_t (void *data_, size_t size_, - free_fn *ffn_) - { - int rc = zs_msg_init_data (this, data_, size_, ffn_); - assert (rc == 0); - } - - // Destroys the message. - inline ~message_t () - { - int rc = zs_msg_close (this); - assert (rc == 0); - } - - // Destroys old content of the message and allocates buffer for the - // new message body. Having this as a separate function allows user - // to reuse once-allocated message for multiple times. - inline void rebuild (size_t size_) - { - int rc = zs_msg_close (this); - assert (rc == 0); - rc = zs_msg_init_size (this, size_); - assert (rc == 0); - } - - // Same as above, however, the message is rebuilt from the supplied - // buffer. See appropriate constructor for discussion of buffer - // deallocation mechanism. - inline void rebuild (void *data_, size_t size_, free_fn *ffn_) - { - int rc = zs_msg_close (this); - assert (rc == 0); - rc = zs_msg_init_data (this, data_, size_, ffn_); - assert (rc == 0); - } - - // Moves the message content from one message to the another. If the - // destination message have contained data prior to the operation - // these get deallocated. The source message will contain 0 bytes - // of data after the operation. - inline void move_to (message_t *msg_) - { - int rc = zs_msg_move (this, (zs_msg*) msg_); - assert (rc == 0); - } - - // Copies the message content from one message to the another. If the - // destination message have contained data prior to the operation - // these get deallocated. - inline void copy_to (message_t *msg_) - { - int rc = zs_msg_copy (this, (zs_msg*) msg_); - assert (rc == 0); - } - - // Returns message type. - inline message_type_t type () - { - return (message_type_t) (1 << zs_msg_type (this)); - } - - // Returns pointer to message's data buffer. - inline void *data () - { - return zs_msg_data (this); - } - - // Returns the size of message data buffer. - inline size_t size () - { - return zs_msg_size (this); - } - - private: - - // Disable implicit message copying, so that users won't use shared - // messages (less efficient) without being aware of the fact. - message_t (const message_t&); - void operator = (const message_t&); - }; - - class context_t - { - friend class socket_t; - - public: - - inline context_t (int app_threads_, int io_threads_) - { - ptr = zs_init (app_threads_, io_threads_); - assert (ptr); - } - - inline ~context_t () - { - int rc = zs_term (ptr); - assert (rc == 0); - } - - private: - - void *ptr; - - // Disable copying. - context_t (const context_t&); - void operator = (const context_t&); - }; - - class socket_t - { - public: - - inline socket_t (context_t &context_, int type_ = 0) - { - ptr = zs_socket (context_.ptr, type_); - assert (ptr); - } - - inline ~socket_t () - { - int rc = zs_close (ptr); - assert (rc == 0); - } - - inline void bind (const char *addr_, zs_opts *opts_ = NULL) - { - int rc = zs_bind (ptr, addr_, opts_); - assert (rc == 0); - } - - inline void connect (const char *addr_, zs_opts *opts_ = NULL) - { - int rc = zs_connect (ptr, addr_, opts_); - assert (rc == 0); - } - - inline void subscribe (const char *criteria_) - { - int rc = zs_subscribe (ptr, criteria_); - assert (rc == 0); - } - - inline void send (message_t &msg_, int flags_ = 0) - { - int rc = zs_send (ptr, &msg_, flags_); - assert (rc == 0); - } - - inline void flush () - { - int rc = zs_flush (ptr); - assert (rc == 0); - } - - inline void recv (message_t *msg_, int flags_ = 0) - { - int rc = zs_recv (ptr, msg_, flags_); - assert (rc == 0); - } - - private: - - void *ptr; - - // Disable copying. - socket_t (const socket_t&); - void operator = (const socket_t&); - }; - -} - -#endif diff --git a/src/Makefile.am b/src/Makefile.am index bb648ec..e6d09ca 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,6 @@ -lib_LTLIBRARIES = libzs.la +lib_LTLIBRARIES = libzmq.la -libzs_la_SOURCES = \ +libzmq_la_SOURCES = \ app_thread.hpp \ atomic_bitmap.hpp \ atomic_counter.hpp \ @@ -109,10 +109,10 @@ libzs_la_SOURCES = \ zmq_decoder.cpp \ zmq_encoder.cpp \ zmq_tcp_engine.cpp \ - zs.cpp + zmq.cpp -libzs_la_LDFLAGS = -version-info 0:0:0 -libzs_la_CXXFLAGS = -Wall -pedantic -Werror @ZS_EXTRA_CXXFLAGS@ +libzmq_la_LDFLAGS = -version-info 0:0:0 +libzmq_la_CXXFLAGS = -Wall -pedantic -Werror @ZMQ_EXTRA_CXXFLAGS@ dist-hook: -rm $(distdir)/src/platform.hpp diff --git a/src/app_thread.cpp b/src/app_thread.cpp index ca08976..2406dbd 100644 --- a/src/app_thread.cpp +++ b/src/app_thread.cpp @@ -17,9 +17,9 @@ along with this program. If not, see . */ -#include "../include/zs.h" +#include "../include/zmq.h" -#if defined ZS_HAVE_WINDOWS +#if defined ZMQ_HAVE_WINDOWS #include "windows.hpp" #else #include @@ -48,17 +48,17 @@ // system with x86 architecture and gcc or MSVC compiler. #if (defined __GNUC__ && (defined __i386__ || defined __x86_64__)) ||\ (defined _MSC_VER && (defined _M_IX86 || defined _M_X64)) -#define ZS_DELAY_COMMANDS +#define ZMQ_DELAY_COMMANDS #endif -zs::app_thread_t::app_thread_t (dispatcher_t *dispatcher_, int thread_slot_) : +zmq::app_thread_t::app_thread_t (dispatcher_t *dispatcher_, int thread_slot_) : object_t (dispatcher_, thread_slot_), tid (0), last_processing_time (0) { } -void zs::app_thread_t::shutdown () +void zmq::app_thread_t::shutdown () { // Deallocate all the sessions associated with the thread. while (!sessions.empty ()) @@ -67,17 +67,17 @@ void zs::app_thread_t::shutdown () delete this; } -zs::app_thread_t::~app_thread_t () +zmq::app_thread_t::~app_thread_t () { } -void zs::app_thread_t::attach_session (session_t *session_) +void zmq::app_thread_t::attach_session (session_t *session_) { session_->set_index (sessions.size ()); sessions.push_back (session_); } -void zs::app_thread_t::detach_session (session_t *session_) +void zmq::app_thread_t::detach_session (session_t *session_) { // O(1) removal of the session from the list. sessions_t::size_type i = session_->get_index (); @@ -86,22 +86,22 @@ void zs::app_thread_t::detach_session (session_t *session_) sessions.pop_back (); } -zs::i_poller *zs::app_thread_t::get_poller () +zmq::i_poller *zmq::app_thread_t::get_poller () { - zs_assert (false); + zmq_assert (false); } -zs::i_signaler *zs::app_thread_t::get_signaler () +zmq::i_signaler *zmq::app_thread_t::get_signaler () { return &pollset; } -bool zs::app_thread_t::is_current () +bool zmq::app_thread_t::is_current () { return !sessions.empty () && tid == getpid (); } -bool zs::app_thread_t::make_current () +bool zmq::app_thread_t::make_current () { // If there are object managed by this slot we cannot assign the slot // to a different thread. @@ -112,7 +112,7 @@ bool zs::app_thread_t::make_current () return true; } -zs::i_api *zs::app_thread_t::create_socket (int type_) +zmq::i_api *zmq::app_thread_t::create_socket (int type_) { i_mux *mux = NULL; i_demux *demux = NULL; @@ -120,43 +120,43 @@ zs::i_api *zs::app_thread_t::create_socket (int type_) i_api *api = NULL; switch (type_) { - case ZS_P2P: + case ZMQ_P2P: mux = new dummy_aggregator_t; - zs_assert (mux); + zmq_assert (mux); demux = new dummy_distributor_t; - zs_assert (demux); + zmq_assert (demux); session = new session_t (this, this, mux, demux, true, false); - zs_assert (session); + zmq_assert (session); api = new p2p_t (this, session); - zs_assert (api); + zmq_assert (api); break; - case ZS_PUB: + case ZMQ_PUB: demux = new data_distributor_t; - zs_assert (demux); + zmq_assert (demux); session = new session_t (this, this, mux, demux, true, false); - zs_assert (session); + zmq_assert (session); api = new pub_t (this, session); - zs_assert (api); + zmq_assert (api); break; - case ZS_SUB: + case ZMQ_SUB: mux = new fair_aggregator_t; - zs_assert (mux); + zmq_assert (mux); session = new session_t (this, this, mux, demux, true, false); - zs_assert (session); + zmq_assert (session); api = new sub_t (this, session); - zs_assert (api); + zmq_assert (api); break; - case ZS_REQ: + case ZMQ_REQ: // TODO - zs_assert (false); + zmq_assert (false); api = new req_t (this, session); - zs_assert (api); + zmq_assert (api); break; - case ZS_REP: + case ZMQ_REP: // TODO - zs_assert (false); + zmq_assert (false); api = new rep_t (this, session); - zs_assert (api); + zmq_assert (api); break; default: errno = EINVAL; @@ -168,14 +168,14 @@ zs::i_api *zs::app_thread_t::create_socket (int type_) return api; } -void zs::app_thread_t::process_commands (bool block_) +void zmq::app_thread_t::process_commands (bool block_) { ypollset_t::signals_t signals; if (block_) signals = pollset.poll (); else { -#if defined ZS_DELAY_COMMANDS +#if defined ZMQ_DELAY_COMMANDS // Optimised version of command processing - it doesn't have to check // for incoming commands each time. It does so only if certain time // elapsed since last command processing. Command delay varies diff --git a/src/app_thread.hpp b/src/app_thread.hpp index 61e7ff1..ffe5596 100644 --- a/src/app_thread.hpp +++ b/src/app_thread.hpp @@ -17,8 +17,8 @@ along with this program. If not, see . */ -#ifndef __ZS_APP_THREAD_HPP_INCLUDED__ -#define __ZS_APP_THREAD_HPP_INCLUDED__ +#ifndef __ZMQ_APP_THREAD_HPP_INCLUDED__ +#define __ZMQ_APP_THREAD_HPP_INCLUDED__ #include @@ -27,7 +27,7 @@ #include "object.hpp" #include "ypollset.hpp" -namespace zs +namespace zmq { class app_thread_t : public object_t, public i_thread @@ -36,7 +36,7 @@ namespace zs app_thread_t (class dispatcher_t *dispatcher_, int thread_slot_); - // To be called when the whole infrastrucure is being closed (zs_term). + // To be called when the whole infrastrucure is being closed. void shutdown (); // Returns signaler associated with this application thread. diff --git a/src/atomic.hpp b/src/atomic.hpp index e24b719..e581593 100644 --- a/src/atomic.hpp +++ b/src/atomic.hpp @@ -17,24 +17,24 @@ along with this program. If not, see . */ -#ifndef __ZS_ATOMIC_HPP_INCLUDED__ -#define __ZS_ATOMIC_HPP_INCLUDED__ +#ifndef __ZMQ_ATOMIC_HPP_INCLUDED__ +#define __ZMQ_ATOMIC_HPP_INCLUDED__ #include "stdint.hpp" -#if defined ZS_FORCE_MUTEXES -#define ZS_ATOMIC_MUTEX +#if defined ZMQ_FORCE_MUTEXES +#define ZMQ_ATOMIC_MUTEX #elif (defined __i386__ || defined __x86_64__) && defined __GNUC__ -#define ZS_ATOMIC_X86 +#define ZMQ_ATOMIC_X86 #elif defined ZMQ_HAVE_WINDOWS -#define ZS_ATOMIC_WINDOWS +#define ZMQ_ATOMIC_WINDOWS #elif defined ZMQ_HAVE_SOLARIS -#define ZS_ATOMIC_SOLARIS +#define ZMQ_ATOMIC_SOLARIS #else -#define ZS_ATOMIC_MUTEX +#define ZMQ_ATOMIC_MUTEX #endif -namespace zs +namespace zmq { // Atomic assignement. @@ -56,11 +56,11 @@ namespace zs // Atomic addition. Returns the old value. inline uint32_t atomic_uint32_add (volatile uint32_t *p_, uint32_t delta_) { -#if defined ZS_ATOMIC_WINDOWS +#if defined ZMQ_ATOMIC_WINDOWS return InterlockedExchangeAdd ((LONG*) &value, increment_); -#elif defined ZS_ATOMIC_SOLARIS +#elif defined ZMQ_ATOMIC_SOLARIS return atomic_add_32_nv (&value, increment_) - delta_; -#elif defined ZS_ATOMIC_X86 +#elif defined ZMQ_ATOMIC_X86 uint32_t old; __asm__ volatile ( "lock; xadd %0, %1\n\t" @@ -80,13 +80,13 @@ namespace zs // Atomic subtraction. Returns the old value. inline uint32_t atomic_uint32_sub (volatile uint32_t *p_, uint32_t delta_) { -#if defined ZS_ATOMIC_WINDOWS +#if defined ZMQ_ATOMIC_WINDOWS LONG delta = - ((LONG) delta_); return InterlockedExchangeAdd ((LONG*) &value, delta); -#elif defined ZS_ATOMIC_SOLARIS +#elif defined ZMQ_ATOMIC_SOLARIS int32_t delta = - ((int32_t) delta_); return atomic_add_32_nv (&value, delta) + delta_; -#elif defined ZS_ATOMIC_X86 +#elif defined ZMQ_ATOMIC_X86 uint32_t old = -delta_; __asm__ volatile ("lock; xaddl %0,%1" : "=r" (old), "=m" (*p_) @@ -116,11 +116,11 @@ namespace zs template inline void *atomic_ptr_xchg (volatile T **p_, T *value_) { -#if defined ZS_ATOMIC_WINDOWS +#if defined ZMQ_ATOMIC_WINDOWS return InterlockedExchangePointer (p_, value_); -#elif defined ZS_ATOMIC_SOLARIS +#elif defined ZMQ_ATOMIC_SOLARIS return atomic_swap_ptr (p_, value_); -#elif defined ZS_ATOMIC_X86 +#elif defined ZMQ_ATOMIC_X86 void *old; __asm__ volatile ( "lock; xchg %0, %2" @@ -144,11 +144,11 @@ namespace zs template inline void *atomic_ptr_cas (volatile T **p_, T *cmp_, T *value_) { -#if defined ZS_ATOMIC_WINDOWS +#if defined ZMQ_ATOMIC_WINDOWS return InterlockedCompareExchangePointer (p_, value_, cmp_); -#elif defined ZS_ATOMIC_SOLARIS +#elif defined ZMQ_ATOMIC_SOLARIS return atomic_cas_ptr (p_, cmp_, value_); -#elif defined ZS_ATOMIC_X86 +#elif defined ZMQ_ATOMIC_X86 void *old; __asm__ volatile ( "lock; cmpxchg %2, %3" @@ -167,7 +167,7 @@ namespace zs #endif } -#if defined ZS_ATOMIC_X86 && defined __x86_64__ +#if defined ZMQ_ATOMIC_X86 && defined __x86_64__ typedef uint64_t atomic_bitmap_t; #else typedef uint32_t atomic_bitmap_t; @@ -187,7 +187,7 @@ namespace zs inline bool atomic_bitmap_btsr (volatile atomic_bitmap_t *p_, int set_index_, int reset_index_) { -#if defined ZS_ATOMIC_WINDOWS +#if defined ZMQ_ATOMIC_WINDOWS while (true) { atomic_bitmap_t oldval = *p_; atomic_bitmap_t newval = (oldval | (atomic_bitmap_t (1) << @@ -197,7 +197,7 @@ namespace zs return (oldval & (atomic_bitmap_t (1) << reset_index_)) ? true : false; } -#elif defined ZS_ATOMIC_SOLARIS +#elif defined ZMQ_ATOMIC_SOLARIS while (true) { atomic_bitmap_t oldval = *p_; atomic_bitmap_t newval = (oldval | (atomic_bitmap_t (1) << @@ -206,7 +206,7 @@ namespace zs return (oldval & (atomic_bitmap_t (1) << reset_index_)) ? true : false; } -#elif defined ZS_ATOMIC_X86 +#elif defined ZMQ_ATOMIC_X86 atomic_bitmap_t oldval, dummy; __asm__ volatile ( "mov %0, %1\n\t" @@ -236,11 +236,11 @@ namespace zs inline atomic_bitmap_t atomic_bitmap_xchg (volatile atomic_bitmap_t *p_, atomic_bitmap_t newval_) { -#if defined ZS_ATOMIC_WINDOWS +#if defined ZMQ_ATOMIC_WINDOWS return InterlockedExchange ((volatile LONG*) p_, newval_); -#elif defined ZS_ATOMIC_SOLARIS +#elif defined ZMQ_ATOMIC_SOLARIS return atomic_swap_32 (p_, newval_); -#elif defined ZS_ATOMIC_X86 +#elif defined ZMQ_ATOMIC_X86 atomic_bitmap_t oldval = newval_; __asm__ volatile ( "lock; xchg %0, %1" @@ -263,7 +263,7 @@ namespace zs inline atomic_bitmap_t atomic_bitmap_izte (volatile atomic_bitmap_t *p_, atomic_bitmap_t thenval_, atomic_bitmap_t elseval_) { -#if defined ZS_ATOMIC_WINDOWS +#if defined ZMQ_ATOMIC_WINDOWS while (true) { atomic_bitmap_t oldval = *p_; atomic_bitmap_t newval = (oldval ? elseval_ : thenval_); @@ -271,14 +271,14 @@ namespace zs oldval) == (LONG) oldval) return oldval; } -#elif defined ZS_ATOMIC_SOLARIS +#elif defined ZMQ_ATOMIC_SOLARIS while (true) { atomic_bitmap_t oldval = *p_; atomic_bitmap_t newval = (oldval ? elseval_ : thenval_); if (atomic_cas_32 (p_, oldval, newval) == oldval) return oldval; } -#elif defined ZS_ATOMIC_X86 +#elif defined ZMQ_ATOMIC_X86 atomic_bitmap_t oldval; atomic_bitmap_t dummy; __asm__ volatile ( diff --git a/src/atomic_bitmap.hpp b/src/atomic_bitmap.hpp index a5440de..6b7218e 100644 --- a/src/atomic_bitmap.hpp +++ b/src/atomic_bitmap.hpp @@ -17,8 +17,8 @@ along with this program. If not, see . */ -#ifndef __ZS_ATOMIC_BITMAP_HPP_INCLUDED__ -#define __ZS_ATOMIC_BITMAP_HPP_INCLUDED__ +#ifndef __ZMQ_ATOMIC_BITMAP_HPP_INCLUDED__ +#define __ZMQ_ATOMIC_BITMAP_HPP_INCLUDED__ #include "stdint.hpp" #include "platform.hpp" @@ -26,29 +26,29 @@ // These are the conditions to choose between different implementations // of atomic_bitmap. -#if defined ZS_FORCE_MUTEXES -#define ZS_ATOMIC_BITMAP_MUTEX +#if defined ZMQ_FORCE_MUTEXES +#define ZMQ_ATOMIC_BITMAP_MUTEX #elif (defined __i386__ || defined __x86_64__) && defined __GNUC__ -#define ZS_ATOMIC_BITMAP_X86 +#define ZMQ_ATOMIC_BITMAP_X86 #elif 0 && defined __sparc__ && defined __GNUC__ -#define ZS_ATOMIC_BITMAP_SPARC -#elif defined ZS_HAVE_WINDOWS -#define ZS_ATOMIC_BITMAP_WINDOWS -#elif defined ZS_HAVE_SOLARIS -#define ZS_ATOMIC_BITMAP_SOLARIS +#define ZMQ_ATOMIC_BITMAP_SPARC +#elif defined ZMQ_HAVE_WINDOWS +#define ZMQ_ATOMIC_BITMAP_WINDOWS +#elif defined ZMQ_HAVE_SOLARIS +#define ZMQ_ATOMIC_BITMAP_SOLARIS #else -#define ZS_ATOMIC_BITMAP_MUTEX +#define ZMQ_ATOMIC_BITMAP_MUTEX #endif -#if defined ZS_ATOMIC_BITMAP_MUTEX +#if defined ZMQ_ATOMIC_BITMAP_MUTEX #include "mutex.hpp" -#elif defined ZS_ATOMIC_BITMAP_WINDOWS +#elif defined ZMQ_ATOMIC_BITMAP_WINDOWS #include "windows.hpp" -#elif defined ZS_ATOMIC_BITMAP_SOLARIS +#elif defined ZMQ_ATOMIC_BITMAP_SOLARIS #include #endif -namespace zs +namespace zmq { // This class encapuslates several bitwise atomic operations on unsigned @@ -79,7 +79,7 @@ namespace zs // another one. Returns the original value of the reset bit. inline bool btsr (int set_index_, int reset_index_) { -#if defined ZS_ATOMIC_BITMAP_WINDOWS +#if defined ZMQ_ATOMIC_BITMAP_WINDOWS while (true) { bitmap_t oldval = value; bitmap_t newval = (oldval | (bitmap_t (1) << set_index_)) & @@ -89,7 +89,7 @@ namespace zs return (oldval & (bitmap_t (1) << reset_index_)) ? true : false; } -#elif defined ZS_ATOMIC_BITMAP_SOLARIS +#elif defined ZMQ_ATOMIC_BITMAP_SOLARIS while (true) { bitmap_t oldval = value; bitmap_t newval = (oldval | (bitmap_t (1) << set_index_)) & @@ -98,7 +98,7 @@ namespace zs return (oldval & (bitmap_t (1) << reset_index_)) ? true : false; } -#elif defined ZS_ATOMIC_BITMAP_X86 +#elif defined ZMQ_ATOMIC_BITMAP_X86 bitmap_t oldval, dummy; __asm__ volatile ( "mov %0, %1\n\t" @@ -112,7 +112,7 @@ namespace zs : "r" (bitmap_t(set_index_)), "r" (bitmap_t(reset_index_)) : "cc"); return (bool) (oldval & (bitmap_t(1) << reset_index_)); -#elif defined ZS_ATOMIC_BITMAP_SPARC +#elif defined ZMQ_ATOMIC_BITMAP_SPARC volatile bitmap_t* valptr = &value; bitmap_t set_val = bitmap_t(1) << set_index_; bitmap_t reset_val = ~(bitmap_t(1) << reset_index_); @@ -132,7 +132,7 @@ namespace zs : "r" (valptr) : "cc"); return oldval; -#elif defined ZS_ATOMIC_BITMAP_MUTEX +#elif defined ZMQ_ATOMIC_BITMAP_MUTEX sync.lock (); bitmap_t oldval = value; value = (oldval | (bitmap_t (1) << set_index_)) & @@ -148,18 +148,18 @@ namespace zs inline bitmap_t xchg (bitmap_t newval_) { bitmap_t oldval; -#if defined ZS_ATOMIC_BITMAP_WINDOWS +#if defined ZMQ_ATOMIC_BITMAP_WINDOWS oldval = InterlockedExchange ((volatile LONG*) &value, newval_); -#elif defined ZS_ATOMIC_BITMAP_SOLARIS +#elif defined ZMQ_ATOMIC_BITMAP_SOLARIS oldval = atomic_swap_32 (&value, newval_); -#elif defined ZS_ATOMIC_BITMAP_X86 +#elif defined ZMQ_ATOMIC_BITMAP_X86 oldval = newval_; __asm__ volatile ( "lock; xchg %0, %1" : "=r" (oldval) : "m" (value), "0" (oldval) : "memory"); -#elif defined ZS_ATOMIC_BITMAP_SPARC +#elif defined ZMQ_ATOMIC_BITMAP_SPARC oldval = value; volatile bitmap_t* ptrin = &value; bitmap_t tmp; @@ -176,7 +176,7 @@ namespace zs : "r" (ptrin) : "cc"); return prev; -#elif defined ZS_ATOMIC_BITMAP_MUTEX +#elif defined ZMQ_ATOMIC_BITMAP_MUTEX sync.lock (); oldval = value; value = newval_; @@ -193,7 +193,7 @@ namespace zs inline bitmap_t izte (bitmap_t thenval_, bitmap_t elseval_) { -#if defined ZS_ATOMIC_BITMAP_WINDOWS +#if defined ZMQ_ATOMIC_BITMAP_WINDOWS while (true) { bitmap_t oldval = value; bitmap_t newval = oldval == 0 ? thenval_ : elseval_; @@ -201,14 +201,14 @@ namespace zs newval, oldval) == (LONG) oldval) return oldval; } -#elif defined ZS_ATOMIC_BITMAP_SOLARIS +#elif defined ZMQ_ATOMIC_BITMAP_SOLARIS while (true) { bitmap_t oldval = value; bitmap_t newval = oldval == 0 ? thenval_ : elseval_; if (atomic_cas_32 (&value, oldval, newval) == oldval) return oldval; } -#elif defined ZS_ATOMIC_BITMAP_X86 +#elif defined ZMQ_ATOMIC_BITMAP_X86 bitmap_t oldval; bitmap_t dummy; __asm__ volatile ( @@ -225,7 +225,7 @@ namespace zs : "r" (thenval_), "r" (elseval_) : "cc"); return oldval; -#elif defined ZS_ATOMIC_BITMAP_SPARC +#elif defined ZMQ_ATOMIC_BITMAP_SPARC volatile bitmap_t* ptrin = &value; bitmap_t tmp; bitmap_t prev; @@ -242,7 +242,7 @@ namespace zs : "r" (ptrin), "r" (thenval_), "r" (elseval_) : "cc"); return prev; -#elif defined ZS_ATOMIC_BITMAP_MUTEX +#elif defined ZMQ_ATOMIC_BITMAP_MUTEX sync.lock (); bitmap_t oldval = value; value = oldval ? elseval_ : thenval_; @@ -256,7 +256,7 @@ namespace zs private: volatile bitmap_t value; -#if defined ZS_ATOMIC_BITMAP_MUTEX +#if defined ZMQ_ATOMIC_BITMAP_MUTEX mutex_t sync; #endif @@ -267,20 +267,20 @@ namespace zs } // Remove macros local to this file. -#if defined ZS_ATOMIC_BITMAP_WINDOWS -#undef ZS_ATOMIC_BITMAP_WINDOWS +#if defined ZMQ_ATOMIC_BITMAP_WINDOWS +#undef ZMQ_ATOMIC_BITMAP_WINDOWS #endif -#if defined ZS_ATOMIC_BITMAP_SOLARIS -#undef ZS_ATOMIC_BITMAP_SOLARIS +#if defined ZMQ_ATOMIC_BITMAP_SOLARIS +#undef ZMQ_ATOMIC_BITMAP_SOLARIS #endif -#if defined ZS_ATOMIC_BITMAP_X86 -#undef ZS_ATOMIC_BITMAP_X86 +#if defined ZMQ_ATOMIC_BITMAP_X86 +#undef ZMQ_ATOMIC_BITMAP_X86 #endif -#if defined ZS_ATOMIC_BITMAP_SPARC -#undef ZS_ATOMIC_BITMAP_SPARC +#i