root/ompi/mca/sharedfp/lockedfile/sharedfp_lockedfile_request_position.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mca_sharedfp_lockedfile_request_position

   1 /*
   2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2005 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2013-2015 University of Houston. All rights reserved.
  13  * $COPYRIGHT$
  14  *
  15  * Additional copyrights may follow
  16  *
  17  * $HEADER$
  18  */
  19 
  20 
  21 #include "ompi_config.h"
  22 #include "sharedfp_lockedfile.h"
  23 
  24 #include "mpi.h"
  25 #include "ompi/constants.h"
  26 #include "ompi/mca/sharedfp/sharedfp.h"
  27 #include "ompi/mca/sharedfp/base/base.h"
  28 
  29 /*Use fcntl to lock the hidden file which stores the current position*/
  30 #include <fcntl.h>
  31 #include <unistd.h>
  32 
  33 int mca_sharedfp_lockedfile_request_position(struct mca_sharedfp_base_data_t * sh,
  34                                              int bytes_requested,
  35                                              OMPI_MPI_OFFSET_TYPE *offset)
  36 {
  37     int ret = OMPI_SUCCESS;
  38     /*file descriptor for the hidden file that we want to lock*/
  39     int fd;
  40     /* flock structure that is used to setup the desired fcntl operation */
  41     struct flock fl;
  42     OMPI_MPI_OFFSET_TYPE position = 0;
  43     /*buffer to use when reading from the locked file*/
  44     OMPI_MPI_OFFSET_TYPE buf;
  45     /*int count = 1;*/
  46 
  47     struct mca_sharedfp_lockedfile_data * lockedfile_data = sh->selected_module_data;
  48     int handle = lockedfile_data->handle;
  49 
  50     *offset = 0;
  51 
  52     /* get the saved file descriptor,
  53      * the hidden file was opened during sharedfp_lockedfile_file_open function */
  54     fd = handle;
  55 
  56     /*set up the flock structure*/
  57     fl.l_type   = F_WRLCK;
  58     fl.l_whence = SEEK_SET;
  59     fl.l_start  = 0;
  60     fl.l_len    = 0;
  61     fl.l_pid    = getpid();
  62 
  63 
  64     /* Aquire an exclusive lock */
  65     if (fcntl(fd, F_SETLKW, &fl) == -1) {
  66         opal_output(0,"sharedfp_lockedfile_request_position: errorr acquiring lock: fcntl(%d,F_SETLKW,&fl)\n",fd);
  67         opal_output(0,"sharedfp_lockedfile_request_position: error(%i): %s", errno, strerror(errno));
  68         return OMPI_ERROR;
  69     }
  70     else{
  71         if ( mca_sharedfp_lockedfile_verbose ) {
  72             opal_output(ompi_sharedfp_base_framework.framework_output,
  73                         "sharedfp_lockedfile_request_position: Success: acquired lock.for fd: %d\n",fd);
  74         }
  75     }
  76 
  77     /* read from the file */
  78     lseek ( fd, 0, SEEK_SET );
  79     read ( fd, &buf, sizeof(OMPI_MPI_OFFSET_TYPE));
  80     if ( mca_sharedfp_lockedfile_verbose ) {
  81         opal_output(ompi_sharedfp_base_framework.framework_output,
  82                     "sharedfp_lockedfile_request_position: Read last_offset=%lld! ret=%d\n",buf, ret);
  83     }
  84 
  85     /* increment the position  */
  86     position = buf + bytes_requested;
  87     if ( mca_sharedfp_lockedfile_verbose ) {
  88         opal_output(ompi_sharedfp_base_framework.framework_output,
  89                     "sharedfp_lockedfile_request_position: old_offset=%lld, bytes_requested=%d, new offset=%lld!\n",
  90                     buf,bytes_requested,position);
  91     }
  92 
  93     /* write to the file  */
  94     lseek ( fd, 0, SEEK_SET );
  95     write ( fd, &position, sizeof(OMPI_MPI_OFFSET_TYPE));
  96 
  97     /* unlock the file */
  98     if ( mca_sharedfp_lockedfile_verbose ) {
  99         opal_output(ompi_sharedfp_base_framework.framework_output,
 100                     "sharedfp_lockedfile_request_position: Releasing lock...");
 101     }
 102 
 103     /* NOTE: We thought we could reuse the flock struct
 104     ** and only reset the l_type parameter, but it gave
 105     ** an invalid argument error. On my machine the invalid
 106     ** argument was l_whence. I reset it, but I also decided to
 107     ** reset all of the other parameters to avoid further problems.
 108     */
 109     fl.l_type   = F_UNLCK;  /* set to unlock same region */
 110     fl.l_whence = SEEK_SET;
 111     fl.l_start  = 0;
 112     fl.l_len    = 0;
 113     fl.l_pid    = getpid();
 114 
 115     if (fcntl(fd, F_SETLK, &fl) == -1) {
 116         opal_output(0,"sharedfp_lockedfile_request_position:failed to release lock for fd: %d\n",fd);
 117         opal_output(0,"error(%i): %s", errno, strerror(errno));
 118         return OMPI_ERROR;
 119     }
 120     else {
 121         if ( mca_sharedfp_lockedfile_verbose ) {
 122             opal_output(ompi_sharedfp_base_framework.framework_output,
 123                         "sharedfp_lockedfile_request_position: released lock.for fd: %d\n",fd);
 124         }
 125     }
 126 
 127     /* return position  */
 128     *offset = buf;
 129 
 130     return ret;
 131 }

/* [<][>][^][v][top][bottom][index][help] */