I am wondering whether the Florida State University implementation of the pthread standard is, by any chance, able to handle the recursive mutexes.
Unfortunately the documentation about the FSU implementation is rather poor, and it does not mention of the possibility or not to declare a mutex as recursive.
Trying to declare a mutex as follows:
pthread_mutex_attr mutex_attr;
pthread_mutexattr_init (&mutex_attr);
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex, NULL);
and compiling using the FSU pthreads library, I got this list of errors:
test.c:25: error: `pthread_mutex_attr' undeclared (first use in this function)
test.c:25: error: (Each undeclared identifier is reported only once
test.c:25: error: for each function it appears in.)
test.c:25: error: parse error before "mutex_attr"
test.c:27: error: `mutex_attr' undeclared (first use in this function)
test.c:28: error: `PTHREAD_MUTEX_RECURSIVE' undeclared (first use in this function)
Trying to compile the same code with the (non-FSU) pthread implementation on my machine, it works.
To avoid trivialities, I tell you in advance that, by default, the POSIX mutexes are not recursive.
Should I conclude that there is no way to make use of recursive mutexes with the FSU implementation, or there is another way to achieve these (i.e. another way to declare a mutex as recursive)?
No, the FSU pthreads implementation does not support recursive mutexes. In fact, the latest release has no notion of mutex types. In addition to lacking the PTHREAD_MUTEX_* mutex type names, it also omits the pthread_mutexattr_settype() and pthread_mutexattr_gettype() functions used to manipulate the mutex type.
Related
I wonder what is the fundamental difference between binding and linking when working with Ada code? I couldn't find a good explanation on google and this is why I ask the question.
For the binding process what is the input and what is the output?
What is the relation between binding and linking? I assume binding needs to be done first.
Thanks,
Bogdan.
With GNAT, there are two jobs which the binder performs: first, checking that all the necessary compilations have been done, so that the program’s closure is consistent, and secondly arranging for elaboration to happen (these jobs are needed for any Ada build system, but they may be implemented differently).
When using gnatmake, the first of these jobs is usually superfluous, because gnatmake has already organised all the necessary compilations. It is possible to get this wrong (by, for example, moving a unit to a different library and not deleting its compilation products from the original place) but quite hard!
Elaboration is a feature of Ada that isn’t present in many other languages. There’s explanation at gcc.gnu.org and other places, but for a simple example,
with Foo;
package Bar is
Int : Integer := Foo.Value;
[...]
end Bar;
package Foo is
function Value return Integer;
[...]
end Foo;
we don’t know what Foo.Value is going to return at compile time, and we may not know until run time (what if it reads a value from the command line?), so Foo.Value must be in a fit state to be called before Bar’s initialisation happens.
Bar’s initialisation happens when Bar is elaborated, and likewise for Foo, so it’s gnatbind’s job to recognise this and arrange that Foo is elaborated before Bar.
It does this by emitting calls to packages’ elaboration code in a function (usually called adanit), and a main(), which is to be called by the operating system and calls adainit and then the Ada main program, say program.adb.
gnatmake then calls gnatlink, which takes the gnatbind-generated code, in Ada in files called b-program.ad[sb] or b__program.ad[sb] or b~program.ad[sb] depending on the vintage of the compiler, compiles it, and links it with the program’s closure to produce the final executable.
See the four points listed here: https://docs.adacore.com/gnat_ugn-docs/html/gnat_ugn/gnat_ugn/building_executable_programs_with_gnat.html#binding-with-gnatbind
You could think of it as a built-in make but without the recompilation: it ensures objects are consistent, generates a correct initialization order, compiles it, and passes everything to the linker.
As pointed out, in Ada the program entry point is not your main procedure, but one that performs a safe initialization and then calls your main procedure.
I've been working on some XML utilities in Objective-C, including my own IO stream objects built around FILE * values. In order to do this safely it is important that I call fclose on the file pointer when the stream object is deallocated (if I had not already done so).
My strategy for this (and many other deallocation tasks) has long been to override the finalize method of NSObject. However I just read (and confirmed by testing) that finalize no longer gets called at all during deallocation.
Garbage collection is deprecated in OS X v10.8; instead, you should use Automatic Reference Counting—see Transitioning to ARC Release Notes.
So apparently I've now been through two versions of OS X without realizing that I was hemorrhaging system resources the whole time. I read through those release notes and I could not find any reference to this particular problem. I was unable to find any alternative method for handling deallocation.
I write Objective-C wrappers for C/C++ data types a lot. I need to some way to release unmanaged memory when the wrapper is being deallocated.
Is there any ARC alternative to finalize? And if not what am I supposed to do about malloc'd C/C++ types in my NSObjects?
From the NSObject class reference...
There are probably several ways to implement this introspection feature through macros and code walkers, but is there a simpler (possible, implementation-dependent) way? I'd imagine, invoking and then releasing the debugger could open access to frame stack, but that seems like an overkill too.
What would be some simpler ideas to try?
Macros can take an &env argument that passes in the lexical environment of the calling context. You can then query the lexical environment using these functions: https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node102.html
In particular, check out variable-information and function-information.
I believe there are also implementation-specific ways to get the current lexical environment at run-time.
Is there a set of general rules/guidelines that can help to understand when to prefer pragma Pure, pragma Preelaborate, or something else entirely? The rules and definitions presented in the standard (Ada 2012), are a little heavy-going and I'd be grateful to read something that's a little more clear and geared towards the average case.
If I wanted to be thorough without fully understanding the "why" of it, can I simply try:
Mark the package spec with pragma Pure;
If it doesn't compile, try pragma Preelaborate;
If that fails, then I've done something tricky and either need to pragma Elaborate units on a with-by-with basis, or rethink the package layout.
While this might work (does it?), because it's recommended to mark a package as Pure whenever possible (likewise with Preelaborate), however it seems a bit brain damaged and I'd prefer to understand the process a bit better.
pragma Pure
You should use this on any package which does not have an internal state. It tells the user of the package that calls to any subprograms cannot have side effects, because there is no internal state they could change. So a function declared at library level inside a pure package will always return the same result when called with the same parameters.
The Ada implementation is allowed to cache return values of functions of a pure package, and to omit calls to subroutines if their return values won't be used because of these requirements. However, you can violate the constraints by calling imported subroutines (e.g. from a C library) inside your pure package (these may change some internal state which the Ada compiler doesn't know of). If you're evil, you can even import Ada subroutines from other parts of the software with pragma Import to bypass the requirements of pragma Pure. Needless to say: If you're doing anything like this, don't use pragma Pure.
Edit: To clarify the circumstances when calls may be omitted, let me quote the ARM:
If a library unit is declared pure, then the implementation is permitted to omit a call on a library-level subprogram of the library unit if the results are not needed after the call. Similarly, it may omit such a call and simply reuse the results produced by an earlier call on the same subprogram, provided that none of the parameters are of a limited type, and the addresses and values of all by-reference actual parameters, and the values of all by-copy-in actual parameters, are the same as they were at the earlier call. This permission applies even if the subprogram produces other side effects when called.
GNAT, for example, additionally defines that any subroutines that take a parameter of type System.Address or a type derived from it are not considered pure even if they are defined in a pure package, because the location the address points to may be altered, but GNAT does not know what kind of structure the address points to and therefore cannot run any checks about whether the referenced value of the parameter has been changed.
pragma Preelaborate
This tells the compiler that the package won't execute any code at elaboration time (i.e. before the main procedure starts executing). At elaboration time, the following constructs will execute:
Initialization of library-level variables (this can be a function call)
Initialization of tasks declared at library level (they may start executing before the main procedure does)
Statements in a begin ... end block at library level
You generally should avoid these things if you don't need them. Use pragma Preelaborate wherever possible, it tells the caller that he can safely use the package without executing anything at elaboration time.
If something doesn't compile with one of these pragmas when you think it should, look into why it doesn't compile. It may help you discover problems with your package implementation or structure. Don't just drop the pragma when it doesn't compile. As the constraint affects possible constraints on any packages that depend on yours, you should always choose the strictest applicable pragma.
Elaboration Order Handling in GNAT is a helpful guide. Ideally, the standard rules will suffice for most programs. The pragmas tell the compiler to substitute your elaboration order. They should be applied to solve specific problems, rather than used empirically.
Addendum: #ajb underscores an important distinction among the pragmas. The article cited agrees with the approach outlined in the question (bullets one and two): "Consequently a good rule is to mark units as Pure or Preelaborate if possible, and if this is not possible, mark them as Elaborate_Body if possible." It goes on to discuss situations (bullet three) "where neither of these three pragmas can be used."
i've been trying to find where and how the reference counting for Qt is implemented. The QBasicAtomicInt and QAtomicInt use the ref() and deref() functions which provide an efficient reference counting API. These functions atomically increment and decrement the value but where are these functions implemented in the library? I am trying to understand how exactly Qt implements atomic reference counting. Thank you.
Atomic operations cannot be achieved in plain C++ so they are implemented directly in assembly for each specific processor architecture.
Here is the source for Qt's atomic operations for x86 processors: http://qt.gitorious.org/qt/qt/blobs/4.7/src/corelib/arch/i386/qatomic_i386.s