What is the equivalent of Win32's Kernel32 on iOS?
Otherwise put: What is the lowest-level, fully-documented (by Apple), stable, supported, userland API on iOS for things like:
Managing memory
Async file I/O
Thread management
Wrappers for synchronization primitives & concurrency constructs
So far what I have found has been a mix of a externally-documented mach and BSD API's together miscellaneous headers (OSMalloc.h, for example). Additionally, the link Apple provides on it's Kernel Programming Guide to the "Up-to-date versions of the Mach 3 APIs" is dead
Start with Apple's opensource XNU project. Look to use:
mach (impl)
Thread management
IPC
Virtual memory
Concurrency constructs
Timing
BSD
POSIX implementation
GCD
Async file IO via kqueue/kevent
Networking
Clang language extensions
Wrappers for ldrex, ldaex, strex, stlex, clrex, dmb, dsb, isb
Related
Introduction
Lua implements co-routine based on setjmp/longjmp functions. I found some problems during porting lua to these RTOS environment:
1. Zephyr RTOS: Failed
Heading
MISRA C 2012 Rule 21.4 pointed:
The standard header file <setjmp.h> shall not be used
and when I call setjmp/longjmp functions it suggests that a MPU Fault is occurred although CONFIG_MPU=n is set in prj.conf.
2. Mbed OS: Success
I call lua API in main() and it contains no other thread or task in program. It was OK. But I do not test multi-thread cases.
3. FreeRTOS: Not Tested yet, but likely ok
Opensource project like NodeMCU and Lua RTOS use Lua and FreeRTOS as their basic framework. But I am not sure that they have tested in FreeRTOS multi-task situation.
I found a thread on FreeRTOS Forum about this problem. One of FreeRTOS developer said he did not know. Another people indicated may cause unexpected behaviors in ISR or task context switching due to conflict register change.
My Question
If I disabled all interrupts during setjmp/longjmp usage, could I avoid unexpected behaviors in case of conflicting of setjmp/longjmp and ISR/task switching?
Why do setjmp/longjmp have failed in Zephyr RTOS?
One is GCD and other is Runloop?It is abstract,how to choose one for my TCP connect ?Very thankful,i only find how to use GCDAsyncSocket on github,but dont know how to choose one.
Hope this may help you:
Even if you're on iOS only, there will likely be multi-core iOS devices in the near future. And GCD will be an excellent way to take advantage of the additional resources with GCDAsyncSocket
GCDAsyncSocket
The minimum requirements for GCDAsyncSocket:Mac OS X 10.6+ or iOS 4.0+
GCDAsyncSocket performs much better than AsyncSocket.
Considering off-loading the encryption/descryption (not SSL/TLS) onto another thread and it seems to me that going the way of GCD would be the better alternative.
Specific features of GCDAsyncSocket include:
1. Classic delegate-style support.
2. It allows allows for parallel socket IO and data processing, as well as easy thread-safety.
3. Queued non-blocking reads and writes, with optional timeouts.
4. Automatic support for IPv4 and IPv6.
5. SSL/TLS support.
6. Built upon the latest technologies such as queues and GCD.
7. Self-contained in one class.
You don't need to muck around with streams or sockets. The class handles all of that.
AsyncSocket
The AsyncSocket library is composed of one class, also called AsyncSocket. An instance of AsyncSocket represents one socket, which may be a listen socket or a connect socket.
If you need to support OS versions prior to Mac OS X 10.6+ or iOS 4.0+, then you'll need to stick with AsyncSocket for now.
AsyncSocket provides easy-to-integrate “fire and forget” networking that makes it easy for your application to support networking.
Features include:
• Queued non-blocking reads and writes, with timeouts.
• Automatic socket acceptance.
• Delegate support.
• Run-loop based, not thread based.
• Self-contained in one class. You do not need to muck around with a collection of stream or socket instances. The class handles all of that.
• Support for TCP streams. AsyncSocket does not support UDP or multicast sockets.
• Based on Apple’s own CFSocket and CFStream Carbon APIs.
Reference Reference_GCDAsyncSocket
Reference About AsyncSocket
How does one actually use kqueue() for doing simple async r/w's?
It's inception seems to be as a replacement for epoll(), and select(), and thus the problem it is trying to solve is scaling to listening on large number of file descriptors for changes.
However, if I want to do something like: read data from descriptor X, let me know when the data is ready - how does the API support that? Unless there is a complimentary API for kicking-off non-blocking r/w requests, I don't see a way other than managing a thread pool myself, which defeats the purpose.
Is this simply the wrong tool for the job? Stick with aio?
Aside: I'm not savvy with how modern BSD-based OS internals work - but is kqueue() built on aio or visa-versa? I would imagine it would depend on whether the OS io subsystem system is fundamentally interrupt-driven or polling.
None of the APIs you mention, aside from aio itself, has anything to do with asynchronous IO, as such.
None of select(), poll(), epoll(), or kqueue() are helpful for reading from file systems (or "vnodes"). File descriptors for file system items are always "ready", even if the file system is network-mounted and there is network latency such that a read would actually block for a significant time. Your only choice there to avoid blocking is aio or, on a platform with GCD, dispatch IO.
The use of kqueue() and the like is for other kinds of file descriptors such as sockets, pipes, etc. where the kernel maintains buffers and there's some "event" (like the arrival of a packet or a write to a pipe) that changes when data is available. Of course, kqueue() can also monitor a variety of other input sources, like Mach ports, processes, etc.
(You can use kqueue() for reads of vnodes, but then it only tells you when the file position is not at the end of the file. So, you might use it to be informed when a file has been extended or truncated. It doesn't mean that a read would not block.)
I don't think either kqueue() or aio is built on the other. Why would you think they were?
I used kqueues to adapt a Linux proxy server (based on epoll) to BSD. I set up separate GCD async queues, each using a kqueue to listen on a set of sockets. GCD manages the threads for you.
Is contiki scheduler preemptive? Tinyos is not; there's nanork which i'm not sure of what state its development is in.
Contiki supports preemptive threads. Refer to:
https://github.com/contiki-os/contiki/wiki/Multithreading
Contiki-OS for IoT's supports preemptive multi-threading. In contiki, multi-threading is implemented as a library on top of the event-driven kernel for dynamic loading and replacement of individual services. The library can be linked with applications that require multi-threading. Contiki multithreading library is divided in two parts: (i) a platform independent part (2) platform specific. The platform independent part interfaces to the event kernel and the platform specific part of the library implements stack switching and preemption primitives. Contiki uses protothreads for implementing so called multi-threading. Protothreads are designed for severely memory constraint devices because they are stack-less and lightweight. The main features of protothreads are: very small memory overhead (only two bytes per protothread), no extra stack for a thread, highly portable (i.e., they are fully written in C and hence there is no architecture-specific assembly code). Contiki does not allow interrupt handlers to post new events, no process synchronization is provided in Contiki. The interrupt handler (when required) and the reader functions must be synchronized to avoid race condition. Please have a look at the following link [The Ring Buffer Library] also: https://github.com/contiki-os/contiki/wiki/Libraries
It might be worth noting that the port for the most widely used sensor node, the TelosB, does not support preemption.
I'm looking for an IMAP client library or parser that can support asynchronous I/O. The end goal being I could have dedicated thread(s) do socket I/O (via a poll() loop or similar) and could send data to waiting clients/parsers, as it becomes available. All of the code/libraries I've seen to date (java.mail, Python's imaplib, Thunderbird's C++ IMAP client, many random ones in C, C++) seem to follow the traditional blocking, one-thread-per-socket approach, which won't work for me.
My ideal client or library would behave much like https://github.com/ry/http-parser in that I/O behavior would not be dictated by the IMAP bits. Instead, the IMAP library would deal with buffers/strings and the caller would manage I/O.
The only possibility I've seen so far is libcurl. But, I'm not sure if the API will work and want to look at other possibilities before going too far down that road or inventing my own solution.
I'm open to considering libraries in any programming language.
Twisted (http://twistedmatrix.com/) has an asynchronous IMAP4 client: twisted.mail.imap4.IMAP4Client
People sometimes say that this protocol is difficult to implement, so implementation quality may be an issue. The defunct Chandler project used the twisted IMAP4 client, and its source code contains the comment "This functionality will be enhanced to be a more robust IMAP client in the near future".
I've had great results with node.js for this kind of thing. If listening to a lot of open sockets you'll need to tweak some linux settings to increase limits for the number of open file discriptors but it works great.