chris wrote:Is the readdir() function in newlib.library threadsafe?
struct __dir
{
unsigned long numentries;
unsigned long allocatedentries;
unsigned long currententry;
struct dirent *entries;
};
typedef struct __dir DIR;

salass00 wrote:chris wrote:Is the readdir() function in newlib.library threadsafe?
If you need thread safety you should use readdir_r() instead (or at least never call readdir() from more than one task at once).
edit: couldn't find readdir_r() in newlib or clib2 includes so I guess it doesn't exist. I could swear I had used it before but I was probably confusing it with strtok()/strtok_r(). I guess you'll have to either hope that readdir() is implemented in a threadsafe manner somehow or add some mutex locking around your directory scanning code.
edit 2: looking at the definition of DIR type (used by readdir()) I don't really see why it wouldn't be threadsafe (assuming that you don't access the same directory handle from more than one task of course):
- Code: Select all
struct __dir
{
unsigned long numentries;
unsigned long allocatedentries;
unsigned long currententry;
struct dirent *entries;
};
typedef struct __dir DIR;
xenic wrote:@chris
It seems that newlib opendir() doesn't lock the directory so I wonder what happens when you opendir() a directory that gets deleted by another Process before you call readdir()?
Hmm, good question. I guess it would return no entries. I would also expect readdir_r() to have the same problem.
xenic wrote:@chrisHmm, good question. I guess it would return no entries. I would also expect readdir_r() to have the same problem.
I wrote a little test program for opendir() and the directory is locked and can't be deleted if the program is compiled with clib2. Since the underlying AmigaDOS functions Examine() & Exall() require a lock for input args and the new function ExamineDir() gets a lock on the directory when the context is created by ObtainDirContext() (according to the autodocs). If the AmigaDOS directory functions lock the directory so it can't disappear, it seems sort of sloppy that newlib doesn't do the same.

tonyw wrote:DOS locks the directory as soon as anyone calls IDOS->ObtainDirContext() (that's what the Lock is for). There shouldn't be any contention between competing clients, no matter whether they come through a C lib or directly by calling IDOS->ExamineObject().
Newlib and clib don't have to lock anything, it's all done at a lower level.
chris wrote:That sounds like a bug in newlib.library.
Return to General Developer Support
Users browsing this forum: No registered users and 3 guests