summaryrefslogtreecommitdiff
path: root/file_io/os2
diff options
context:
space:
mode:
authorBrian Havard <bjh@apache.org>2010-03-30 10:35:43 +0000
committerBrian Havard <bjh@apache.org>2010-03-30 10:35:43 +0000
commit2ea737c161153c38fa5f9c826b8bee5b961da8e3 (patch)
tree2872d30f5b4e22b9f9a6e5d53f70958f8dde48e8 /file_io/os2
parent45b199ad97cd3404e9885aa178a3bd6afa20e043 (diff)
downloadapr-2ea737c161153c38fa5f9c826b8bee5b961da8e3.tar.gz
OS/2: Only mutex protect buffer access if APR_FOPEN_XTHREAD is specified.
Also add missing mutex locking in apr_file_flush(). git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@929070 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'file_io/os2')
-rw-r--r--file_io/os2/buffer.c34
-rw-r--r--file_io/os2/open.c12
-rw-r--r--file_io/os2/readwrite.c32
3 files changed, 57 insertions, 21 deletions
diff --git a/file_io/os2/buffer.c b/file_io/os2/buffer.c
index 34e4e6393..4fc73804b 100644
--- a/file_io/os2/buffer.c
+++ b/file_io/os2/buffer.c
@@ -22,35 +22,45 @@ APR_DECLARE(apr_status_t) apr_file_buffer_set(apr_file_t *file,
apr_size_t bufsize)
{
apr_status_t rv;
+ int do_locking = file->mutex != NULL && file->buffered;
- apr_thread_mutex_lock(file->mutex);
+ if (do_locking) {
+ apr_thread_mutex_lock(file->mutex);
+ }
- if(file->buffered) {
+ if (file->buffered) {
/* Flush the existing buffer */
rv = apr_file_flush(file);
if (rv != APR_SUCCESS) {
- apr_thread_mutex_unlock(file->mutex);
+ if (do_locking) {
+ apr_thread_mutex_unlock(file->mutex);
+ }
+
return rv;
}
}
file->buffer = buffer;
file->bufsize = bufsize;
- file->buffered = 1;
file->bufpos = 0;
file->direction = 0;
file->dataRead = 0;
-
- if (file->bufsize == 0) {
- /* Setting the buffer size to zero is equivalent to turning
- * buffering off.
- */
- file->buffered = 0;
+
+ if (bufsize > 0 && file->mutex == NULL && (file->flags & APR_FOPEN_XTHREAD)) {
+ /* buffering is being turned on but we have no mutex, make one */
+ rv = apr_thread_mutex_create(&file->mutex, 0, file->pool);
}
+
+ /* Setting the buffer size to zero is equivalent to turning
+ * buffering off.
+ */
+ file->buffered = file->bufsize > 0;
- apr_thread_mutex_unlock(file->mutex);
+ if (do_locking) {
+ apr_thread_mutex_unlock(file->mutex);
+ }
- return APR_SUCCESS;
+ return rv;
}
APR_DECLARE(apr_size_t) apr_file_buffer_size_get(apr_file_t *file)
diff --git a/file_io/os2/open.c b/file_io/os2/open.c
index a1a55202f..f8afbb1b6 100644
--- a/file_io/os2/open.c
+++ b/file_io/os2/open.c
@@ -36,7 +36,7 @@ APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new, const char *fname, apr
int mflags = OPEN_FLAGS_FAIL_ON_ERROR|OPEN_SHARE_DENYNONE|OPEN_FLAGS_NOINHERIT;
int rv;
ULONG action;
- apr_file_t *dafile = (apr_file_t *)apr_palloc(pool, sizeof(apr_file_t));
+ apr_file_t *dafile = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
dafile->pool = pool;
dafile->isopen = FALSE;
@@ -61,10 +61,14 @@ APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new, const char *fname, apr
if (dafile->buffered) {
dafile->buffer = apr_palloc(pool, APR_FILE_DEFAULT_BUFSIZE);
dafile->bufsize = APR_FILE_DEFAULT_BUFSIZE;
- rv = apr_thread_mutex_create(&dafile->mutex, 0, pool);
- if (rv)
- return rv;
+ if (flag & APR_FOPEN_XTHREAD) {
+ rv = apr_thread_mutex_create(&dafile->mutex, 0, pool);
+
+ if (rv) {
+ return rv;
+ }
+ }
}
if (flag & APR_FOPEN_CREATE) {
diff --git a/file_io/os2/readwrite.c b/file_io/os2/readwrite.c
index 9c44ee0e0..5dc0f9ad6 100644
--- a/file_io/os2/readwrite.c
+++ b/file_io/os2/readwrite.c
@@ -24,6 +24,24 @@
#include <malloc.h>
+static void file_lock(apr_file_t *thefile)
+{
+ if (thefile->mutex && thefile->buffered) {
+ apr_thread_mutex_lock(thefile->mutex);
+ }
+}
+
+
+
+static void file_unlock(apr_file_t *thefile)
+{
+ if (thefile->mutex && thefile->buffered) {
+ apr_thread_mutex_unlock(thefile->mutex);
+ }
+}
+
+
+
APR_DECLARE(apr_status_t) apr_file_read(apr_file_t *thefile, void *buf, apr_size_t *nbytes)
{
ULONG rc = 0;
@@ -39,13 +57,13 @@ APR_DECLARE(apr_status_t) apr_file_read(apr_file_t *thefile, void *buf, apr_size
ULONG blocksize;
ULONG size = *nbytes;
- apr_thread_mutex_lock(thefile->mutex);
+ file_lock(thefile);
if (thefile->direction == 1) {
int rv = apr_file_flush(thefile);
if (rv != APR_SUCCESS) {
- apr_thread_mutex_unlock(thefile->mutex);
+ file_unlock(thefile);
return rv;
}
@@ -79,7 +97,7 @@ APR_DECLARE(apr_status_t) apr_file_read(apr_file_t *thefile, void *buf, apr_size
}
*nbytes = rc == 0 ? pos - (char *)buf : 0;
- apr_thread_mutex_unlock(thefile->mutex);
+ file_unlock(thefile);
if (*nbytes == 0 && rc == 0 && thefile->eof_hit) {
return APR_EOF;
@@ -137,7 +155,7 @@ APR_DECLARE(apr_status_t) apr_file_write(apr_file_t *thefile, const void *buf, a
int blocksize;
int size = *nbytes;
- apr_thread_mutex_lock(thefile->mutex);
+ file_lock(thefile);
if ( thefile->direction == 0 ) {
// Position file pointer for writing at the offset we are logically reading from
@@ -160,7 +178,7 @@ APR_DECLARE(apr_status_t) apr_file_write(apr_file_t *thefile, const void *buf, a
size -= blocksize;
}
- apr_thread_mutex_unlock(thefile->mutex);
+ file_unlock(thefile);
return APR_FROM_OS_ERROR(rc);
} else {
if (thefile->flags & APR_FOPEN_APPEND) {
@@ -288,11 +306,15 @@ APR_DECLARE(apr_status_t) apr_file_flush(apr_file_t *thefile)
int rc = 0;
if (thefile->direction == 1 && thefile->bufpos) {
+ file_lock(thefile);
+
rc = DosWrite(thefile->filedes, thefile->buffer, thefile->bufpos, &written);
thefile->filePtr += written;
if (rc == 0)
thefile->bufpos = 0;
+
+ file_unlock(thefile);
}
return APR_FROM_OS_ERROR(rc);