summaryrefslogtreecommitdiff
path: root/toxcore/tox.h
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-08-10 13:46:04 +0100
committeriphydf <iphydf@users.noreply.github.com>2016-08-17 14:57:20 +0100
commitdd8a568141b43546521ca438d861e00c26051f6c (patch)
tree98f3f9a871f164b20693eca89eb0c8bf2ae826ed /toxcore/tox.h
parentcebf64a588039c202880659a85d070126bcc68da (diff)
Make self_connection_status callback stateless.
**What are we doing?** We are moving towards stateless callbacks. This means that when registering a callback, you no longer pass a user data pointer. Instead, you pass a user data pointer to tox_iterate. This pointer is threaded through the code, passed to each callback. The callback can modify the data pointed at. An extra indirection will be needed if the pointer itself can change. **Why?** Currently, callbacks are registered with a user data pointer. This means the library has N pointers for N different callbacks. These pointers need to be managed by the client code. Managing the lifetime of the pointee can be difficult. In C++, it takes special effort to ensure that the lifetime of user data extends at least beyond the lifetime of the Tox instance. For other languages, the situation is much worse. Java and other garbage collected languages may move objects in memory, so the pointers are not stable. Tox4j goes through a lot of effort to make the Java/Scala user experience a pleasant one by keeping a global array of Tox+userdata on the C++ side, and communicating via protobufs. A Haskell FFI would have to do similarly complex tricks. Stateless callbacks ensure that a user data pointer only needs to live during a single function call. This means that the user code (or language runtime) can move the data around at will, as long as it sets the new location in the callback. **How?** We are doing this change one callback at a time. After each callback, we ensure that everything still works as expected. This means the toxcore change will require 15 Pull Requests.
Diffstat (limited to 'toxcore/tox.h')
-rw-r--r--toxcore/tox.h17
1 files changed, 15 insertions, 2 deletions
diff --git a/toxcore/tox.h b/toxcore/tox.h
index 1655d9c1..ca6869e4 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -84,6 +84,19 @@ extern "C" {
84 * callback will result in no callback being registered for that event. Only 84 * callback will result in no callback being registered for that event. Only
85 * one callback per event can be registered, so if a client needs multiple 85 * one callback per event can be registered, so if a client needs multiple
86 * event listeners, it needs to implement the dispatch functionality itself. 86 * event listeners, it needs to implement the dispatch functionality itself.
87 *
88 * The last argument to a callback is the user data pointer. It is passed from
89 * tox_iterate to each callback in sequence.
90 *
91 * The user data pointer is never stored or dereferenced by any library code, so
92 * can be any pointer, including NULL. Callbacks must all operate on the same
93 * object type. In the apidsl code (tox.in.h), this is denoted with `any`. The
94 * `any` in tox_iterate must be the same `any` as in all callbacks. In C,
95 * lacking parametric polymorphism, this is a pointer to void.
96 *
97 * Old style callbacks that are registered together with a user data pointer
98 * receive that pointer as argument when they are called. They can each have
99 * their own user data pointer of their own type.
87 */ 100 */
88/** \subsection threading Threading implications 101/** \subsection threading Threading implications
89 * 102 *
@@ -776,7 +789,7 @@ typedef void tox_self_connection_status_cb(Tox *tox, TOX_CONNECTION connection_s
776 * 789 *
777 * TODO: how long should a client wait before bootstrapping again? 790 * TODO: how long should a client wait before bootstrapping again?
778 */ 791 */
779void tox_callback_self_connection_status(Tox *tox, tox_self_connection_status_cb *callback, void *user_data); 792void tox_callback_self_connection_status(Tox *tox, tox_self_connection_status_cb *callback);
780 793
781/** 794/**
782 * Return the time in milliseconds before tox_iterate() should be called again 795 * Return the time in milliseconds before tox_iterate() should be called again
@@ -788,7 +801,7 @@ uint32_t tox_iteration_interval(const Tox *tox);
788 * The main loop that needs to be run in intervals of tox_iteration_interval() 801 * The main loop that needs to be run in intervals of tox_iteration_interval()
789 * milliseconds. 802 * milliseconds.
790 */ 803 */
791void tox_iterate(Tox *tox); 804void tox_iterate(Tox *tox, void *user_data);
792 805
793 806
794/******************************************************************************* 807/*******************************************************************************